1
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为空

在此处输入图像描述

但是当我看到 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>
4

1 回答 1

2

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

试试这个:

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);  

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

于 2012-11-06T10:24:54.130 回答