0

我正在尝试遍历 XML 元素并将值分配给对象的成员变量。以下是我尝试过的。然而SelectSingleNode()正在回归NULL。data.Any 是一个XmlElement数组(由服务返回)。我试过使用 a XmlNamespaceManager,但 data.Any 不是 aXmlDocument所以它不包含 a NameTable。我究竟做错了什么?

代码:

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>
4

1 回答 1

1
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;
}
于 2013-07-11T23:46:29.050 回答