如何使用命名空间获取XML元素 [英] How to get XML element with namespace

查看:64
本文介绍了如何使用命名空间获取XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的XML文件:

I have a XML file like this :

我想获取所有标签a:entry

I want to get all the tag a:entry

<?xml version="1.0" encoding="utf-8"?>
<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02">
  <a:link rel="next" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&amp;startIndex=50&amp;chunkSize=50&amp;pagingToken=50%7c50&amp;os=8.0.10512.0&amp;cc=US&amp;oc=&amp;lang=vi-VN&amp;hw=469838850&amp;dm=Virtual&amp;oemId=NOKIA&amp;moId=" />
  <a:link rel="self" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&amp;chunkSize=50&amp;os=8.0.10512.0&amp;cc=US&amp;oc=&amp;lang=vi-VN&amp;hw=469838850&amp;dm=Virtual&amp;oemId=NOKIA&amp;moId=" />
  <os:startIndex>0</os:startIndex>
  <os:totalResults>51</os:totalResults>
  <os:itemsPerPage>50</os:itemsPerPage>
  <a:updated>2013-11-01T08:30:11.711450Z</a:updated>
  <a:title type="text">List Of Items</a:title>
  <a:id>tag:catalog.zune.net,2013-11-01:/apps</a:id>
  <pagingToken>50|50</pagingToken>
  <impressionId>cd11c7b0116143dcb4c99f15b72ebbc4</impressionId>
  <a:entry>
    <a:updated>2013-11-01T08:30:11.711450Z</a:updated>
    <a:title type="text">Facebook</a:title>
    <a:id>urn:uuid:82a23635-5bd9-df11-a844-00237de2db9e</a:id>
    <isHardwareCompatible>true</isHardwareCompatible>
    <sortTitle>Facebook</sortTitle>
    <releaseDate>2010-10-19T13:07:17.103000Z</releaseDate>
    <version>5.1.2.0</version>
    <averageUserRating>5.963479</averageUserRating>
    <userRatingCount>42825</userRatingCount>
    <image>
      <id>urn:uuid:f8b42bcd-45c3-4ea5-9c9e-a108ac33cd6e</id>
    </image>
    <hasLiveTile>true</hasLiveTile>
    <categories>
      <category>
        <id>windowsphone.Social</id>
        <title>mạng xã hội</title>
        <isRoot>True</isRoot>
      </category>
    </categories>
    <tags>
      <tag>Independent</tag>
      <tag>LockScreen_Background</tag>
      <tag>LockScreen_Notification_IconCount</tag>
      <tag>LockScreen_Notification_TextField</tag>
      <tag>phone.protocol.fb</tag>
    </tags>
    <offers>
      <offer>
        <offerId>urn:uuid:5c6b4028-74c5-4648-a71e-2ca413a5d2fd</offerId>
        <mediaInstanceId>urn:uuid:64051c69-fb7b-4972-ad42-1dbb2e626a2c</mediaInstanceId>
        <clientTypes>
          <clientType>WindowsPhone80</clientType>
          <clientType>WindowsPhone81</clientType>
        </clientTypes>
        <paymentTypes>
          <paymentType>Credit Card</paymentType>
          <paymentType>Mobile Operator</paymentType>
        </paymentTypes>
        <store>ZEST</store>
        <price>0</price>
        <displayPrice>$0.00</displayPrice>
        <priceCurrencyCode>USD</priceCurrencyCode>
        <licenseRight>Purchase</licenseRight>
      </offer>
    </offers>
    <publisher>
      <id>Microsoft Corporation</id>
      <name>Microsoft Corporation</name>
    </publisher>
    <impressionId>cd11c7b0116143dcb4c99f15b72ebbc4</impressionId>
    <k>4</k>
  </a:entry>

我的代码:

  var list = from r in xdoc.Descendants("a:entry") select r;

我得到了这个错误:

':'字符(十六进制值0x3A)不能包含在名称.

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

我如何获得a:entry标签?谢谢:)

How I can get the a:entry tag ?? Thanks :)

推荐答案

您需要指定 XNamespace :

XNamespace xmlns = "http://www.w3.org/2005/Atom";

,然后使用接受字符串的 XNamespace 重载的 + 运算符:

and then use XNamespace overloaded + operator, that accepts string:

var list = from r in xdoc.Descendants(xmlns + "entry") select r;

甚至更好

var list = xdoc.Descendants(xmlns + "entry");

例如,要从首个 entry "中选择所有标签,请使用下一个代码作为示例:

For example, in order to select all tags from First entry use next code as an example:

XNamespace entryNamespace = "http://www.w3.org/2005/Atom";
XNamespace tagNamespace = "http://schemas.zune.net/catalog/apps/2008/02";

var tags = xdoc.Descendants(entryNamespace + "entry")
               .First()
               .Descendants(tagNamespace + "tag");

foreach (var tag in tags)
{
    Console.WriteLine (tag.Value);
}

打印:

Independent
LockScreen_Background
LockScreen_Notification_IconCount
LockScreen_Notification_TextField
phone.protocol.fb

这是与名称空间无关的版本(选择相同的 tags ):

here is the namespace agnostic version (selects the same tags):

var tags = xdoc.Descendants()
               .Where(node => node.Name.LocalName == "entry")
               .First()
               .Descendants()
               .Where(node => node.Name.LocalName == "tag");

当然,您始终可以选择xml中的所有标签,而无需指定名称空间或 entry 节点名称:

and you of course can always select all the tags in the xml without specifying neither namespace nor entry node name:

var tags = xdoc.Descendants()
               .Where(node => node.Name.LocalName == "tag");

这篇关于如何使用命名空间获取XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆