给梦一个奔跑的方向!
PDF Print E-mail
User Rating: / 4
PoorBest 
Written by xlingfairy
Friday, 08 May 2009 23:28
先看以下内容:
XML 命名空间将 XML 文档中的元素和属性名称与自定义和预定义的 URI 关联起来。 为命名空间 URI 定义的前缀用来限定 XML 数据中的元素和属性的名称以实现此关联。 命名
空间可防止元素和属性名称冲突,并允许以不同的方式处理和验证同名的元素和属性。http://msdn.microsoft.com/zh-cn/library/c1a6xs06.aspx
任何没有用命名空间前缀完全限定的元素均属于默认命名空间。当在 XML 文档中使用多个命名空间时,将其中一个命名空间定义为默认命名空间可以使文档更加简洁。 只有来自
非默认命名空间的元素需要完全限定。 默认命名空间只适用于元素,不适用于属性。http://msdn.microsoft.com/zh-cn/library/757cbs7s.aspx

如果 XPath 表达式不包含前缀,则假定命名空间统一资源标识符 (URI) 是空的命名空间。 如果 XML 包含默认命名空间,仍必须将前缀和命名空间 URI 添加到
XmlNamespaceManager;否则,不会选择任何节点。http://msdn.microsoft.com/zh-cn/library/d271ytdx.aspx

在取 Google Analytics 的数据时,Google 传回来一段XML,包括多个命名空间,大概如下,我做了删减:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:dxp='http://schemas.google.com/analytics/2009'>
  <updated>2009-04-30T16:59:59.999-07:00</updated>
  <title type='text'>Google Analytics Data for Profile xxxxx</title>
  <generator version='1.0'>Google Analytics</generator>
  <openSearch:totalResults>30</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>30</openSearch:itemsPerPage>
  <dxp:startDate>2009-04-01</dxp:startDate>
  <dxp:endDate>2009-04-30</dxp:endDate>
  <dxp:aggregates>
    <dxp:metric confidenceInterval='0.0' name='ga:pageviews' type='integer' value='926'/>
    <dxp:metric confidenceInterval='0.0' name='ga:uniquePageviews' type='integer' value='842'/>
  </dxp:aggregates>
</feed>
 
可以看到的有 dxp 和 openSearch, 还有一个默认的命名空间。
正如前面所说,如果不加命名空间,dxp, openSearch 这样的节点是取不出来的。
需要这样:
XmlDocument doc = new XmlDocument();
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("dxp","
http://schemas.google.com/analytics/2009");
doc.Load(rep.GetResponseStream());
XmlNode node = doc.SelectSingleNode("//dxp:aggregates/dxp:metric[@name='ga:uniquePageviews']",xnm);
Console.WriteLine("Unique Views: {0}",node.Attributes.GetNamedItem("value").Value);
对于上面所给出的XML,用:
node = doc.SelectSingleNode("//author");
是取不到节点的,node 是 null。这就是默认命名空间的问题。要取得默认命名空间下的节点,还需要多写一点。

XmlDocument doc = new XmlDocument();
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("dxp","http://schemas.google.com/analytics/2009");
xnm.PushScope();
xnm.AddNamespace("GA","
http://www.w3.org/2005/Atom");///////////////////////////
doc.Load(rep.GetResponseStream());
XmlNode node = doc.SelectSingleNode("//dxp:aggregates/dxp:metric[@name='ga:uniquePageviews']",xnm);
Console.WriteLine("Unique Views: {0}",node.Attributes.GetNamedItem("value").Value);
node = doc.SelectSingleNode("//dxp:aggregates/dxp:metric[@name='ga:pageviews']",xnm);
Console.WriteLine("Page Views:{0}",node.Attributes.GetNamedItem("value").Value);
XmlNodeList entries = doc.SelectNodes("//GA:entry",xnm);//////////////////////
Console.WriteLine(entries.Count);
node = doc.SelectSingleNode("//GA:author",xnm);///////////////////////////////
 
嵌套命名空间节的选取:
node = doc.SelectSingleNode("//GA:entry/dxp:dimension",xnm);
Console.WriteLine(node.Attributes.GetNamedItem("name").Value);
 

Add comment


Security code
Refresh

Popular Contents

Recommend

Site Info

Members : 1
Content : 143
Web Links : 7
Content View Hits : 113056

Links

 

Trace