0

I need a little help with reading some XML from a web service. I load the XML into an XML document like this.

XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);

And doc.InnerText shows the text of this node, and all its child nodes. That seems to be working, but its all bunched together. How Would I got about getting the individual elements like first_name or last_name.

I would have assumed something like

foreach (XmlNode node in doc.ChildNodes)
            {
                MessageBox.Show(node["first_name"].Value);
            }

would work, but node["first_name"].Value gives me a null reference exception error.

The XML I'm working with looks like this:

<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="https://www.example.com/">
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="client_id" type="xs:int" minOccurs="0" />
                <xs:element name="first_name" type="xs:string" minOccurs="0" />
                <xs:element name="last_name" type="xs:string" minOccurs="0" />
                <xs:element name="home_phone" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet xmlns="">
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <client_id>1002</client_id>
        <first_name>Sam</first_name>
        <last_name>Spade</last_name>
        <home_phone>5405555555</home_phone>
      </Table>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>
Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 1
    Have you looked at the data you're looping over? `doc.ChildNodes` returns the `` element and the root (``) element. Neither of them has a `` as an immediate child, so yes, your `node["first_name"]` will return null in both cases. You could get it the hard way, by getting `["diffgr:diffgram"]["NewDataSet"]["Table"]["first_name"]`, but you'd probably be better off either looking into XPath to find the element you're looking for, or switch to XDocument and use LINQ to XML instead of the archaic XmlDocument. – Joe White Jan 31 '17 at 22:25
  • `doc.GetElementsByTagName("first_name").Cast().Select(e => e.InnerText).ToList()` – Kalten Jan 31 '17 at 22:30
  • Thanks for the responses. I haven't really worked with XML before, and I formatted the XML (Actually, now that I looked, Joe did it before I did it. Oops. I shoulda done that before I submitted it...), and now its a lot more obvious why I was getting null reference exceptions. – David 'Encore' Merryman Feb 01 '17 at 16:49

0 Answers0