0

I am tasked with ripping and stripping pertinent data from XFDL files. I am attempting to use XmlDocument's SelectSignleNode method to do so. However, it has proven unsuccessful.

Represntative XML:

<XFDL>
...
<page1>
<check3>true</check3>
</page1>
...
<page sid="PAGE1">
<check sid="CHECK9">
<value>true</value>
</check>
</page>
...

Code:

XmlDocument document = new XmlDocument();
document.Load(memoryStream);//decoded and unzipped xfdl file
//Doesn't work
XmlNode checkBox = document.SelectSingleNode("//check[@sid='CHECK9']/value");
//Doesn't work
XmlNode checkBox = document.SelectSingleNode("//page[@sid='PAGE1']/check[@sid='CHECK9']");
MsgBox(checkBox.InnerXml);

Yields me System.NullReferenceException as an XmlNode isn't selected.

I think I'm having an xpath issue but I can't seem to understand where. The earlier xml node is easily selected using:

XmlNode checkBox = document.SelectSingleNode("//page1/check3");
MsgBox(checkBox.InnerText);

Displays just fine. And just to head it off at the pass, there isn't a definition of <check9></check9> in the <page1> tag.

Anyone have some insight?

Thanks in advance.

mcwagner
  • 31
  • 8
  • Are there multiple nodes that could result from the xpath? – Kevin Brown Dec 02 '15 at 17:44
  • The first one: Yes. The second one: No. I don't thin I'm missing a nested path, either. – mcwagner Dec 02 '15 at 18:14
  • Verified fully enumerated xpath is as follows: xfdl > page > check > value via opening XML in IE. What am I missing? – mcwagner Dec 02 '15 at 19:52
  • Good question! If you knew you would not be asking here. Is it possible that the @sid values are not really uppercase ? Your xpaths to elements seem to work and things break when you add the [@sid] qualifier – Kevin Brown Dec 02 '15 at 21:02
  • You're right about it breaking when adding the qualifiers. I can grab all instances of '' using 'GetElementsByTagName' but doesn't want to cooperate with either 'SelectNodes' or 'SelectSingleNode'. I did leave out in my representative XML the namespaces in '' tag. To that end, I have instantiated 'XmlNamespaceManager' and added all namespaces and called 'SelectSingleNode("xpath", manager)' to no additional effect... – mcwagner Dec 03 '15 at 14:28

1 Answers1

0

Okay, so here's the deal. XFDL defines a default namespace that requires an arbitrary mapping for xpath querying. In my case:

XML:

<XFDL xmlns="http://www.ibm.com/xmlns/prod/xfdl/8.0" ... >

Code:

manager.AddNamespace("a", "http://www.ibm.com/xmlns/prod/xfdl/8.0");
//Append 'a:' to query elements
document.SelectSingleNode("//a:check[@sid='CHECK9']/a:value", manager);

The problem is compounded because <check> is buried in <page> which is defined in another namespace: xfdl. My xpath query becomes:

document.SelectSingleNode("//xfdl:page[@sid='PAGE1']/a:check[@sid='CHECK9']/a:value", manager);

Now this is rather XFDL specific but can be applied to other issues where there are multiple namespaces defined within an XML document.

EDIT 1 Source: http://codeclimber.net.nz/archive/2008/01/09/How-to-query-a-XPath-doc-that-has-a-default.aspx

mcwagner
  • 31
  • 8