I want to take input name from user if name math to xml attribute name then it will shows "found" other wise it will show "not found" but in my program on first iteration its show "not found" while on second iteration it shows "found". What the problem in it.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace SalaryReader { class SalaryReader { static void Main(string[] args) { Console.Write("Enter Name of Person: "); //write on console String PersonName = Console.ReadLine(); //take input from user //create reader for XmlSalaries.xml XmlReader reader = XmlReader.Create("C:\\Users\\ZohaiB\\Desktop\\ConsoleApplication1\\XmlSalaries.xml"); while (reader.Read()) { if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Salary")) { string name = reader.GetAttribute("Name"); if (PersonName == name) { Console.WriteLine("Salary of " + PersonName + " is " + reader.GetAttribute("CurrentSalary")); } else if (PersonName != reader.GetAttribute("Name")) { Console.WriteLine("not found"); } } } Console.ReadKey(); //read next key from keyboard and print it } } } While xml file is:
<?xml version="1.0" encoding="utf-8" ?> - <Salaries> <Salary Name="Ahmad" CurrentSalary="20000" /> <Salary Name="Ali" CurrentSalary="40000" /> <Salary Name="Waseem" CurrentSalary="30000" /> <Salary Name="Riaz" CurrentSalary="70000" /> </Salaries>
No comments:
Post a Comment