5

Can anyone provide an example of using the XmlDocument.SelectSingleNodeNS function for WinRT? I'm unclear what the second parameter is expecting and I can't find an example.

public IXmlNode SelectSingleNodeNS(
  string xpath, 
  object namespaces
)

Contains a string that specifies the namespaces to use in XPath expressions when it is necessary to define new namespaces externally. Namespaces are defined in the XML style, as a space-separated list of namespace declaration attributes. You can use this property to set the default namespace as well.

1

1 Answer 1

8

The namespaces parameter is obviously just a string (although declared as object) that must contain an XML namespace declaration in the form "xmlns:aliasname='namespace'" (the XML style). For example

xmlDocument.DocumentElement.SelectNodesNS("cb:person", 
    "xmlns:cb='http://www.addison-wesley.de/codebook'");

works with an XML document like this:

<?xml version="1.0" encoding="utf-8" ?>
<persons xmlns="http://www.addison-wesley.de/codebook">
  <person id="1000">
    <firstname>Zaphod</firstname>
    <lastname>Beeblebrox</lastname>
    <type>Alien</type>
  </person>
...
</persons> 

Note that the alias (cb:) was used in the XPath.

If the namespace is not in the XML style you get the infamous COM error E_Fail.

The (poor) documentation of SelectNodesNS says: "Contains a string that specifies namespaces for use in XPath expressions when it is necessary to define new namespaces externally. Namespaces are defined in the XML style, as a space-separated list of namespace declaration attributes. You can use this property to set the default namespace as well."

According to that namespaces must be a string and could contain more than one XML namespace (did not try that yet). Still the question is open why it is an object.

2
  • I read MSDN's "contains a string" and assumed the parameter was some kind of complex object, or array, or something, that had an array item or property of some undocumented name, that was of type string, that had the namespace URI. What a nightmare; took me an hour of Googling to figure out what to do with it, but your excellent answer was it. Thank you.
    – Jon Davis
    Jan 10, 2013 at 21:44
  • May I hook in here and ask you if you managed to use xpath 2.0 function like xs:date with WinRT XML code? I am getting: 0x80004005 - JavaScript runtime error: Unknown method. //CatalogItem[@code='101'and -->xs:date('2011-09-22T00:00:00.000Z')<-- ge xs:date(@validFrom) and xs:date('2011-09-22T00:00:00.000Z') le xs:date(@validTo)]
    – philk
    Mar 6, 2014 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.