tags:

views:

37

answers:

1

So, in the XML below using this code sample, i can successfully grab HomeAddress and Name information for each entry from my XML. What if I also need to grab the ID (drivers license numberxxx) information from each entry?

EDIT: Updated XML sample for clarificaiton

Code Sample:

XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(responseFromServer);
        XmlNamespaceManager xnsmgr = new XmlNamespaceManager(xDoc.NameTable);
        xnsmgr.AddNamespace("ns1", "http://www.w3.org/2005/Atom");
        xnsmgr.AddNamespace("ns2", "http://strange.com/ns/1.0/");
        XmlNodeList xnlInsuredListMembers = xDoc.SelectNodes("//ns2:InsuredListMember", xnsmgr);
        foreach (XmlNode xnMember in xnlInsuredListMembers)
        {
            XmlNode xnHomeAddress = xnMember.SelectSingleNode("ns2:HomeAddress", xnsmgr);
            string sHomeAddress = xnHomeAddress.InnerText;

            XmlNode xnName = xnMember.SelectSingleNode("ns2:Name", xnsmgr);
            string sName = xnName.InnerText;

            MessageBox.Show(sHomeAddress + sName);
        }

XML Sample

<?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom"&gt;
      <id>someID</id>
      <title type="text">Title</title>
      <author>
        <name>XML Author</name>
      </author>
      <updated>2010-10-25T20:05:30.267Z</updated>
      <link href="currentURL"></link>
      <link href="nextURL"></link>
      <entry>
        <id>Drivers License Numberxxx</id>
        <content type="application/vnd.ctct+xml">
          <InsuredListMember xmlns="http://strange.com/ns/1.0/"&gt;
            <HomeAddress>123 anystreet</HomeAddress>
            <Name>doe, somegal</Name>
          </InsuredListMember>
        </content>
      </entry>
      <entry>
        <id>Drivers License Numberxxx</id>
        <content type="application/vnd.ctct+xml">
          <InsuredListMember xmlns="http://strange.com/ns/1.0/"&gt;
            <HomeAddress>321 anystreet</HomeAddress>
            <Name>doe, someguy</Name>
          </InsuredListMember>
        </content>
      </entry>
    </feed>
+2  A: 

First, this //ns2:ContactListMember should be //ns2:InsuredListMember according to your input sample.

Second, if the context node is some ns2:InsuredListMember, the the id attribute is selected by this expresion: @id.

If you want the ns1:id child of ns1:entry for the given ns2:InsuredListMember, this XPath expression: ../../ns1:id

Alejandro
thanks, have corrected the code sample. gets hairy when working with multiple versions =X Also, have updated the xml to simplify what i'm looking for. I don't need the ID attribute of insuredlistmember, i need the id field of the entry element, (feed/entry/id)
Ares Desmoulins
@Ares Demoulins: Check my edit.
Alejandro
+1 for a correct answer.
Dimitre Novatchev
Thank you Alejandro! You're a lifesaver!
Ares Desmoulins
@Ares Demoulins: You are wellcome! But "lifesaver"? I wonder what your work environment! Ja!
Alejandro