Reputation: 3035
I am trying to loop through an XML element and assign the values to member variables of an object. Below is what I have tried. however SelectSingleNode()
is returning NULL
. data.Any is a XmlElement
array (returned by a service). I have tried using a XmlNamespaceManager
, but data.Any is not a XmlDocument
so it does not contain a NameTable
. What am I doing wrong?
CODE:
foreach (XmlElement item in data.Any)
{
result.CorrelationID = item.SelectSingleNode("CorrelationID").InnerText;
}
XML:
<CorrelationID xmlns="http://www.host.com/folder/anotherFolder">9B36D7A7EDD26A22</CorrelationID>
<EmployerRef xmlns="http://www.host.com/folder/anotherFolder">1235/AN612</EmployerRef>
<Name xmlns="http://www.host.com/folder/anotherFolder">
<Ttl>MS</Ttl>
<Fore>NameFirst</Fore>
<Sur>NameLast</Sur>
</Name>
<PayId xmlns="http://www.host.com/folder/anotherFolder">FLDA/12</PayId>
<NINOToUse xmlns="http://www.host.com/folder/anotherFolder">SL3747A</NINOToUse>
<MessageID xmlns="http://www.host.com/folder/anotherFolder">3</MessageID>
Upvotes: 0
Views: 907
Reputation: 54907
foreach (XmlElement item in data.Any)
{
XmlDocument doc = item.OwnerDocument;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("af", "http://www.host.com/folder/anotherFolder");
result.CorrelationID = item.SelectSingleNode("af::CorrelationID", nsmgr).InnerText;
}
Upvotes: 1