91

I have this XML (stored in a C# string 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'd like to browse all <sog> elements. For each of them, I'd like to print the child <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 seems I can't read a string as XML? I get System.ArgumentException

markzzz
  • 47,390
  • 120
  • 299
  • 507

5 Answers5

121

You should use LoadXml method, not Load:

xmlDoc.LoadXml(myXML); 

Load method is trying to load xml from a file and LoadXml from a string. You could 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);
} 
Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
  • Awesome easy. Finally I can start working with APIs :). – C4d Jul 03 '16 at 23:20
  • 9
    Instead of `SelectSingleNode("//field1").Value` it should be `SelectSingleNode("//field1").InnerText` or `SelectSingleNode("//field1").InnerXml`, because Value would be null, since it is not an attribute, but the value is between the tags. – Mathias Conradt Jul 21 '16 at 14:17
  • 3
    To add to @MathiasConradt, it should be `SelectSingleNode("field1").InnerText` if you don't want to always read the `field1` of the first occurence of myDataz/listS/sog. – Matthieu M. Apr 12 '18 at 10:27
22

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 <sog> tag.

 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);
   }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
15

The other answers are several years old (and do not work for Windows Phone 8.1) so I figured I'd drop in another option. I used this to parse an RSS response for a Windows Phone app:

XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(xml_string);
sraboy
  • 903
  • 2
  • 13
  • 26
3

Or use the XmlSerializer class.

XmlSerializer xs = new XmlSerializer(objectType);
obj = xs.Deserialize(new StringReader(yourXmlString));
teebot
  • 1,089
  • 2
  • 12
  • 25
2

I used the System.Xml.Linq.XElement for the purpose. Just check code below for reading the value of first child node of the xml(not the 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;
MGR
  • 313
  • 3
  • 14