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.

4

1 回答 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.

于 2012-11-11T07:07:55.630 回答