user228777
user228777

Reputation: 3094

XmlNodeList.SelectSingleNode() returns null when elements are present

In the following code, SelectSingleNode is returning 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);
}

In this case the value of elemList.Item(i) is as below, I see page/value node in OuterXml.

elemList.Item(i.OuterXml) is as below.

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

Upvotes: 1

Views: 3615

Answers (3)

Aradhana
Aradhana

Reputation: 1

I have read this Question very late but giving solution so it might be help anyone else who is struggling with null value for selectedNodes or SelectedSingleNode.

XmlDocument xmlresp = GetXMLWebRequest(URL, PayLoad, "");

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

XmlNodeList elemList = xmlresp.SelectSingleNode("Document");

Please update your code with bold line and try.

Upvotes: 0

Steve Guidi
Steve Guidi

Reputation: 20198

The problem is that both the <Page> and <Value> elements are under the http://integration.fiapi.com namespace (it is applied to all descendants of <Document>), and you haven't specified a namespace in your SelectSingleNode query.

You can remove the namespace, or modify the query as follows:

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
}

Upvotes: 1

dotnetnate
dotnetnate

Reputation: 769

Give this a shot:

XmlNamespaceManager nsManager;

nsManager = new XmlNamespaceManager(doc.NameTable);

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

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

Upvotes: 1

Related Questions