Or *_*r A 5 .net xml xpath xmldocument
我有一个带有各种命名空间的xml,我想使用它来查询.SelectNodes(string xPath)
我注意到的问题是,只要我拥有所有这些名称空间,xPath查询就不会返回任何内容.
无论如何告诉XmlDocument.SelectNodes忽略这些命名空间,只是给我正确的元素(我查询的元素似乎没有名称空间前缀)?
如果有,有谁可以请我提供一个如何做的例子?我在查询节点之前/当我应该定义什么?
谢谢您的帮助.
更正:我仍然无法确定问题所在.这是我的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>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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);
Run Code Online (Sandbox Code Playgroud)
我的节点数仍为0!任何的想法?
您需要创建命名空间管理器,设置要使用的所有命名空间及其前缀,然后在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);
Run Code Online (Sandbox Code Playgroud)
警告:我没有编译此代码.