当元素存在时,XmlNodeList.SelectSingleNode()返回null

时间:2011-08-31 17:13:46

标签: c# .net xml xpath

在以下代码中,SelectSingleNode返回null。

XmlDocument xmlresp = GetXMLWebRequest(URL, PayLoad, "");
XmlNodeList elemList = xmlresp.GetElementsByTagName("Document");
for (int i = 0; i < elemList.Count - 1; i++)
{
   byte[] b = System.Convert.FromBase64String(elemList.Item(i).SelectSingleNode("Page/Value").Value);
}

在这种情况下,elemList.Item(i)的值如下所示,我在OuterXml中看到了页面/值节点。

elemList.Item(i.OuterXml)如下所示。

<Document xmlns=\"http://integration.fiapi.com\">
  <ID>1</ID>
  <Date>2006-03-19</Date>
  <Class>Check</Class>
  <Type>GetImage</Type>
  <Page Number=\"1\" Format=\"jpg\" View=\"F\">
    <Value>
      /9j/4AAQSkZJRgABAgIAyQDJAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCALgBogBAREA/8QA0gAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoLEAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy
WccE3hnSr2Rc5nnkhDvkk8hJlXjpwB09easf8JD8c/+hM0P/v8AL/8AJFH/AAkPxz/6EzQ/+/y//JFZepXvx/vrhZbfTLTT0CBTFataMpOT8x8x3OecdccDjrmn/wAZD/5/s+oLqy/aCvPI81518mVZl8meziyw6BthG5eeVbKnuDV+xuP2grS8jnmsoL2Nc5gnNmEfII5KMrcdeCOnpxVc/D74yazLNqU3ij+ypLqV5TYjVrgCDLH5VCblC+gDHAx3rQtfAvxps9OnsYvHFi0M27c01zJLIMjB2yPCXXgcbSMHkYPNZ/8Awqz4v/8AQ+/+Vi7/APiKP+FWfF//AKH3/wArF3/8RWhd+APi/d6Pp2nf8JbYw/YfN/0mHUrtZrje2796235tvRemBWf/AMKs+L//AEPv/lYu/wD4ij/hVnxf/wCh9/8AKxd//EUf8Ks+L/8A0Pv/AJWLv/4ij/hVnxf/AOh9/wDKxd//ABFH/CrPi/8A9D7/AOVi7/8AiKuab8J/ibLcMuqfEe7toNhKva31zOxbI4KsUAGM857DjnjU/wCFQeMP+is65+U3/wAfo/4VB4w/6Kzrn5Tf/H6P+FQeMP8AorOuflN/8frn/wDhmX/qbv8Aym//AG2j/hmX/qbv/Kb/APbaP+GZf+pu/wDKb/8AbauSfs96lNpcOly+Prt9PhffFaNZsYkbnlU87APzNy
ayST+GXluDE1xkZ2vuhXAIUBoWORkEuTj1sfAPwlC/wAPtXu9RhkaDXXa3aMyDbJboGTI2/MpLPKp5B+UEY6nkPCvia28J/CHx34Z1ARx61FcSwG1knRS5lVYG2YJLlCjM2BjGOecjU8F6PZ/CD4tWuna5q0Df2johLXJxFDBIX3FSzsMr+5IDcEll+UVb8BQTX3wo+I/iu7ikiu9
    </Value>
  </Page>
</Document>

3 个答案:

答案 0 :(得分:1)

问题是<Page><Value>元素都在http://integration.fiapi.com命名空间下(它适用于<Document>的所有后代),而你还没有在SelectSingleNode查询中指定了一个名称空间。

您可以删除命名空间,或按如下方式修改查询:

XmlNamespaceManager mgr = new XmlNamespaceManager(xmlresp.NameTable);
mgr.AddNamespace("ns", "http://integration.fiapi.com");

for (int i = 0; i < elemList.Count; ++i)
{
    XmlNode node = elemList[i].SelectSingleNode("ns:Page/ns:Value", mgr);
    // ... convert node.Value as needed
}

答案 1 :(得分:1)

试一试:

XmlNamespaceManager nsManager;

nsManager = new XmlNamespaceManager(doc.NameTable);

nsManager.AddNamespace("fi", "http://integration.fiapi.com");

elementList.Item(i).SelectSingleNode("fi:Page/fi:Value", nsManager);

答案 2 :(得分:0)

我已经很晚才读到这个问题,但是给出了解决方案,所以它可能对其他为selectedNodes或SelectedSingleNode的值为空的人有所帮助。

XmlDocument xmlresp = GetXMLWebRequest(URL,PayLoad,“”);

xmlresp.Load(new xmlTextReader(new StringReader(“ Document)){Namespaces = false});

XmlNodeList elemList = xmlresp.SelectSingleNode(“ Document”);

请用粗体行更新代码,然后尝试。