XPath for XML with a namespace

I have xml with various namespaces that I would like to request using .SelectNodes (xPath string)

The problem I notice is that the xPath query does not return anything as long as I have all of these namespaces.

  • is there anyway to tell XmlDocument.SelectNodes to ignore these namespaces and just get me the correct elements (the elements I request do not seem to have a namespace prefix)?

  • If there is, can someone give me an example of how to do this? What should I determine before / when I request nodes?

Thanks for the help.

Correction: I still cannot understand what the problem is. here is my xml:

<feed xmlns="http://www.w3.org/2005/Atom"  xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"  xmlns:gf="http://schemas.google.com/finance/2007"  
      xmlns:gd="http://schemas.google.com/g/2005" >
  <id>http://finance.google.com/finance/feeds/xyx@google.com/portfolios</id>
  <updated>2009-12-15T19:32:21.000Z</updated>
  <category scheme="http://schemas.google.com/g/2005#kind"  term="http://schemas.google.com/finance/2007#portfolio" />
  <title type="text" >Portfolio Feed</title>
  <link rel="alternate"  type="text/html"  href="http://finance.google.com/finance/portfolio?action=view" />
  <link rel="http://schemas.google.com/g/2005#feed"  type="application/atom+xml"  href="http://finance.google.com/finance/feeds/default/portfolios" />
  <link rel="http://schemas.google.com/g/2005#post"  type="application/atom+xml"  href="http://finance.google.com/finance/feeds/default/portfolios" />
  <link rel="self"  type="application/atom+xml"  href="http://finance.google.com/finance/feeds/default/portfolios" />
  <openSearch:totalResults>24</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>24</openSearch:itemsPerPage>
  <entry>
    <id>http://finance.google.com/finance/feeds/xyx@google.com/portfolios/2</id>
    <updated>2009-12-14T16:26:53.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind"  term="http://schemas.google.com/finance/2007#portfolio" />
    <title type="text" >Main</title>
    <link rel="self"  type="application/atom+xml"  href="http://finance.google.com/finance/feeds/default/portfolios/2" />
    <link rel="edit"  type="application/atom+xml"  href="http://finance.google.com/finance/feeds/default/portfolios/2" />
    <gd:feedLink href="http://finance.google.com/finance/feeds/xyx@google.com/portfolios/2/positions" />
    <gf:portfolioData currencyCode="USD"  gainPercentage="0.0"  return1w="0.0"  return1y="0.0"  return3m="0.0"  return3y="0.0"  return4w="0.0"  return5y="0.0"  returnOverall="0.0"  returnYTD="0.0" />
  </entry>
</feed>

and here is my code:

XmlDocument xml = ExecuteRequest (url);

        var xmlnsManager = new System.Xml.XmlNamespaceManager(xml.NameTable);
        xmlnsManager.AddNamespace("xmlns:openSearch", "http://a9.com/-/spec/opensearchrss/1.0/");
        xmlnsManager.AddNamespace("xmlns:gf", "http://schemas.google.com/finance/2007");
        xmlnsManager.AddNamespace("xmlns:gd", "http://schemas.google.com/g/2005");

        var nodes = xml.SelectNodes("//feed/entry", xmlnsManager);

and the number of nodes in it is 0! any idea?

+5
3
-1

, , , , XPath, .

var doc = new XmlDocument(); 
doc.Load("myfile.xml");

var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("ns", "http://example.org/schema.xsd");

doc.SelectNodes("//ns:MyElement",xmlnsManager);

: .

+9

.NET api, XPATH, , (, *) local-name .

. /*[local-name()='foo']/*[local-name()='bar']/*[local-name()='baz'] ns:foo/ns:bar/ns:baz ns.

, XPATH.

Obviously, with XPATH statements that do not contain a namespace, you can get unintended results (if there is mixed contents of the namespace with elements with the same name), and XPATH is really verbose.

In XPATH 2.0, you can use wildcards for namespaces: /*:foo/*:bar/*:bazbut you will need to use Saxon to get support for XSLT / XPATH 2.0 in .NET.

+2
source

All Articles