Pergunta

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?

After execution of above code nodeList is empty

enter image description here

But when I see the XMLDocument it contains the the required Nodes entry

Here is the the 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>
Foi útil?

Solução

There's a known issue in .NET with the default XML namespace - contrary to what is defined in the XML standards, in .NET you cannot use a string.Empty as the prefix - you need to use something else.

Try this:

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

Now, this list does have two nodes in it - right?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top