A very simple problem with XML
-
Hi, I am new to manipulating XML with C#. I want to know how to retrieve an element value. I have this vey simple XML file : <?xml version="1.0" encoding="utf-8"?> <people> <person> <Lname> Washington </Lname> <FName> George </Fname> </person> <person> <Lname> Lincoln </Lname> <Fname> Abraham </Fname> </person> </people> All I need is a simple method : public string GetFname(string Lname) { ... } which returns the first name by last name. Thanks a lot!
-
Hi, I am new to manipulating XML with C#. I want to know how to retrieve an element value. I have this vey simple XML file : <?xml version="1.0" encoding="utf-8"?> <people> <person> <Lname> Washington </Lname> <FName> George </Fname> </person> <person> <Lname> Lincoln </Lname> <Fname> Abraham </Fname> </person> </people> All I need is a simple method : public string GetFname(string Lname) { ... } which returns the first name by last name. Thanks a lot!
hi, There are some different ways how you can do that. Maybe easiest (but not necessary most effective) is this:
XmlDocument doc = new XmlDocument(); doc.Load( fileNameofYourXml ) while( doc.Read() ) // read XML node by node { if( doc.Name == "Lname" && doc.Value == Lname ) { doc.Read(); // read following node (Fname node) return doc.Value; // return its value } }
OR you can go with XPath:
doc.SelectSingleNode( string.Format("/person/following-sibling::Lname='{0}'", Lname) );
But I bet I got syntax for that XPath expresion wrong :-O Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
hi, There are some different ways how you can do that. Maybe easiest (but not necessary most effective) is this:
XmlDocument doc = new XmlDocument(); doc.Load( fileNameofYourXml ) while( doc.Read() ) // read XML node by node { if( doc.Name == "Lname" && doc.Value == Lname ) { doc.Read(); // read following node (Fname node) return doc.Value; // return its value } }
OR you can go with XPath:
doc.SelectSingleNode( string.Format("/person/following-sibling::Lname='{0}'", Lname) );
But I bet I got syntax for that XPath expresion wrong :-O Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidyHi, thanks for your reply, In fact I tried the first method. It doesn't work. 1-XmlDocument has no method Read, (only a method ReadNode which take a reader as argument! No idea how it works) 2-Instead I used XmlTextReader, still not working. One remark: when I display all the Values and Names in a label, I get many of them null. Other think, they are always the same : value=name? Any hint? For the Xpath method didn't try it. Thanks.
-
Hi, thanks for your reply, In fact I tried the first method. It doesn't work. 1-XmlDocument has no method Read, (only a method ReadNode which take a reader as argument! No idea how it works) 2-Instead I used XmlTextReader, still not working. One remark: when I display all the Values and Names in a label, I get many of them null. Other think, they are always the same : value=name? Any hint? For the Xpath method didn't try it. Thanks.
benqazou wrote: 1-XmlDocument has no method Read, (only a method ReadNode which take a reader as argument! No idea how it works) Oh yes, sorry. My bad. :-O Of course Read() is method of XmlReader. benqazou wrote: 2-Instead I used XmlTextReader, still not working. One remark: when I display all the Values and Names in a label, I get many of them null. Other think, they are always the same : value=name? Can you post snippet of your code? Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
Hi, I am new to manipulating XML with C#. I want to know how to retrieve an element value. I have this vey simple XML file : <?xml version="1.0" encoding="utf-8"?> <people> <person> <Lname> Washington </Lname> <FName> George </Fname> </person> <person> <Lname> Lincoln </Lname> <Fname> Abraham </Fname> </person> </people> All I need is a simple method : public string GetFname(string Lname) { ... } which returns the first name by last name. Thanks a lot!
Hi, since it is a simple requirement, I have placed a simple solution Try this.. ////////////////////////////////////////// using System; using System.Xml; namespace ConsoleApplication1 { /* c:\pk.xml Washington George Lincoln Abraham */ class XmlTest { XmlDocument doc;// public XmlTest() { doc = new XmlDocument(); doc.Load("c:\\pk.xml"); // put your file path } public void Run() { while(true) { Console.Write("\nEnter a Name: "); string input = Console.ReadLine(); if(input == "" || input==null) break; Console.WriteLine("Your FName is " + GetFName(input)); } } public string GetFName(string lname) { string ret = "Unknown" ; XmlNodeList lnode = doc.GetElementsByTagName("Lname"); for(int i=0; i< lnode.Count; i++) { if(lnode.Item(i).InnerText.Trim() == lname.Trim()) { ret = lnode.Item(i).ParentNode.LastChild.InnerText; break; } } return ret; } public static void Main() { XmlTest xmlDoc = new XmlTest(); xmlDoc.Run(); } } } love2code