即使xml包含节点,SelectNodes也会给出空节点

时间:2012-11-06 10:15:06

标签: c# xml selectnodes

XPathNavigator nav = xmlDoc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace(string.Empty,@"http://www.w3.org/2005/Atom");
nsMgr.AddNamespace("dxp",@"http://schemas.google.com/analytics/2009");
nsMgr.AddNamespace("openSearch",@"http://a9.com/-/spec/opensearch/1.1/");


XmlNodeList nodeList = xmlDoc.SelectNodes("entry",nsMgr);  // nodeList is empty why?

执行上述代码后nodeList为空

enter image description here

但是当我看到XMLDocument时,它包含了所需的节点entry

这是XMLDocument innerXML

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dxp="http://schemas.google.com/analytics/2009" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
  <id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&amp;dimensions=ga:visitorType&amp;metrics=ga:visitors&amp;start-date=2012-10-06&amp;end-date=2012-11-06</id>
  <updated>2012-11-06T10:04:40.613Z</updated>
  <title type="text">Google Analytics Data for Profile 63294209</title>
  <link rel="self" type="application/atom+xml" href="https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&amp;dimensions=ga:visitorType&amp;metrics=ga:visitors&amp;start-date=2012-10-06&amp;end-date=2012-11-06" />
  <author>
    <name>Google Analytics</name>
  </author>
  <generator>Google Analytics</generator>
  <openSearch:totalResults>2</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>1000</openSearch:itemsPerPage>
  <dxp:aggregates>
    <dxp:metric name="ga:visitors" type="integer" value="6709" />
  </dxp:aggregates>
  <dxp:containsSampledData>false</dxp:containsSampledData>
  <dxp:dataSource>
    <dxp:property name="ga:profileId" value="63294209" />
    <dxp:property name="ga:webPropertyId" value="UA-34279407-1" />
    <dxp:property name="ga:accountName" value="The Federal Savings Bank" />
    <dxp:tableId>ga:63294209</dxp:tableId>
    <dxp:tableName>The Federal Savings Bank</dxp:tableName>
  </dxp:dataSource>
  <dxp:endDate>2012-11-06</dxp:endDate>
  <dxp:startDate>2012-10-06</dxp:startDate>
  <entry>
    <id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&amp;ga:visitorType=New+Visitor&amp;start-date=2012-10-06&amp;end-date=2012-11-06</id>
    <updated>2012-11-06T10:04:40.613Z</updated>
    <title type="text">ga:visitorType=New Visitor</title>
    <link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
    <dxp:dimension name="ga:visitorType" value="New Visitor" />
    <dxp:metric name="ga:visitors" type="integer" value="5240" />
  </entry>
  <entry>
    <id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&amp;ga:visitorType=Returning+Visitor&amp;start-date=2012-10-06&amp;end-date=2012-11-06</id>
    <updated>2012-11-06T10:04:40.613Z</updated>
    <title type="text">ga:visitorType=Returning Visitor</title>
    <link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
    <dxp:dimension name="ga:visitorType" value="Returning Visitor" />
    <dxp:metric name="ga:visitors" type="integer" value="1469" />
  </entry>
</feed>

1 个答案:

答案 0 :(得分:2)

.NET中存在一个已知问题,默认的XML命名空间 - 与XML标准中定义的相反,在.NET中,您不能使用string.Empty作为前缀 - 您需要使用其他东西。< / p>

试试这个:

XPathNavigator nav = xmlDoc.CreateNavigator();

XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace("def", @"http://www.w3.org/2005/Atom");  <== Give this a prefix!
nsMgr.AddNamespace("dxp", @"http://schemas.google.com/analytics/2009");
nsMgr.AddNamespace("openSearch", @"http://a9.com/-/spec/opensearch/1.1/");

XmlNodeList nodeList = xmlDoc.SelectNodes("/def:feed/def:entry", nsMgr);  

现在,这个列表中确实有两个节点 - 对吗?