2012-11-06 3 views
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? 

포함 된 경우에도 빈 노드를 제공 nodeListSelectNodes는 XML을 위의 코드를 실행 한 후 노드

enter image description here

비어하지만이 XMLDocument로 볼 때 그것은 필요한 노드를 포함 entry

여기에 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> 

답변

2

.NET에서 기본 XML 네임 스페이스와 관련하여 알려진 문제가 있습니다. XML 표준에 정의 된 것과 달리. NET에서는 string.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); 

을 지금,이 목록은 두 개의 노드를 가지고있다?

+1

입니다. 고맙습니다. 정말로 구조화되었습니다. – Champ

+0

@ marc_s 나는 비슷한 질문을한다. 확인해 주시겠습니까? http://stackoverflow.com/questions/13247449/customize-xml-serialize-with-new-tags-and-attributes-and-root – Saeid