Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectNodes即使xml包含空节点,也会为空节点赋值_C#_Xml_Selectnodes - Fatal编程技术网

C# SelectNodes即使xml包含空节点,也会为空节点赋值

C# SelectNodes即使xml包含空节点,也会为空节点赋值,c#,xml,selectnodes,C#,Xml,Selectnodes,执行上述代码后,nodeList为空 但是当我看到XMLDocument时,它包含所需的节点条目 以下是xml文档innerXML XPathNavigator nav = xmlDoc.CreateNavigator(); XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable); nsMgr.AddNamespace(string.Empty,@"http://www.w3.org/2005/Atom"); nsM

执行上述代码后,
nodeList
为空

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

以下是
xml文档innerXML

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?

https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&尺寸=ga:visitorType&;指标=ga:访客和;开始日期=2012-10-06&;结束日期=2012-11-06
2012-11-06T10:04:40.613Z
个人资料63294209的谷歌分析数据
谷歌分析
谷歌分析
2.
1.
1000
假的
ga:63294209
联邦储蓄银行
2012-11-06
2012-10-06
https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&ga:visitorType=新建+访客&;开始日期=2012-10-06&;结束日期=2012-11-06
2012-11-06T10:04:40.613Z
ga:visitorType=新访客
https://www.googleapis.com/analytics/v2.4/data?ids=ga:63294209&ga:visitorType=返回+访客&;开始日期=2012-10-06&;结束日期=2012-11-06
2012-11-06T10:04:40.613Z
ga:visitorType=返回的访问者

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

试试这个:

<?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>
XPathNavigator nav=xmlDoc.CreateNavigator();
XmlNamespaceManager nsMgr=新的XmlNamespaceManager(nav.NameTable);

nsMgr.AddNamespace(“def”,@”http://www.w3.org/2005/Atom"); 这确实奏效了。谢谢一个男人。我问了一个类似的问题,请你检查一下,这里:
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);