Read the XML (from a string) and get some fields - Problems reading XML

I have this XML (stored in a C # line called myXML )

 <?xml version="1.0" encoding="utf-16"?> <myDataz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <listS> <sog> <field1>123</field1> <field2>a</field2> <field3>b</field3> </sog> <sog> <field1>456</field1> <field2>c</field2> <field3>d</field3> </sog> </listS> </myDataz> 

and I would like to see all the <sog> elements. For each of them, I would like to print a child of <field1> .

So this is my code:

 XmlDocument xmlDoc = new XmlDocument(); string myXML = "<?xml version=\"1.0\" encoding=\"utf-16\"?><myDataz xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><listS><sog><field1>123</field1><field2>a</field2><field3>b</field3></sog><sog><field1>456</field1><field2>c</field2><field3>d</field3></sog></listS></myDataz>" xmlDoc.Load(myXML); XmlNodeList parentNode = xmlDoc.GetElementsByTagName("listS"); foreach (XmlNode childrenNode in parentNode) { HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//field1").Value); } 

but it seems I can not read the string as XML? I get a System.ArgumentException

+48
c # xml parsing xml-parsing
Dec 06 2018-11-11T00:
source share
5 answers

You should use the LoadXml method, not Load:

 xmlDoc.LoadXml(myXML); 

The load method attempts to load xml from a file and LoadXml from a string. You can also use XPath:

 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); string xpath = "myDataz/listS/sog"; var nodes = xmlDoc.SelectNodes(xpath); foreach (XmlNode childrenNode in nodes) { HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//field1").Value); } 
+67
Dec 6 2018-11-12T00:
source share

Use Linq-XML,

 XDocument doc = XDocument.Load(file); var result = from ele in doc.Descendants("sog") select new { field1 = (string)ele.Element("field1") }; foreach (var t in result) { HttpContext.Current.Response.Write(t.field1); } 

OR: Get the node list of the <sog> .

  XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(myXML); XmlNodeList parentNode = xmlDoc.GetElementsByTagName("sog"); foreach (XmlNode childrenNode in parentNode) { HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("field1").InnerText); } 
+14
Dec 06 2018-11-11T00:
source share

The rest of the answers are several years old (and do not work for Windows Phone 8.1), so I decided that I would take another option. I used this to parse the RSS response for a Windows Phone application:

 XDocument xdoc = new XDocument(); xdoc = XDocument.Parse(xml_string); 
+5
Sep 06 '15 at 6:46
source share

Or use the XmlSerializer class.

 XmlSerializer xs = new XmlSerializer(objectType); obj = xs.Deserialize(new StringReader(yourXmlString)); 
+3
Dec 06 2018-11-11T00:
source share

I used System.Xml.Linq.XElement for this purpose. Just check the code below to read the value of the first child node element for xml (not root node).

  string textXml = "<xmlroot><firstchild>value of first child</firstchild>........</xmlroot>"; XElement xmlroot = XElement.Parse(textXml); string firstNodeContent = ((System.Xml.Linq.XElement)(xmlroot.FirstNode)).Value; 
+1
Jun 01 '16 at 12:20
source share



All Articles