Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What format is expected by the namespaces parameter in SelectSingleNodeNS

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.

like image 287
Josh Rickard Avatar asked Nov 10 '12 19:11

Josh Rickard


1 Answers

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.

like image 108
Jürgen Bayer Avatar answered Oct 29 '22 13:10

Jürgen Bayer