简体   繁体   English

如何使用两个名称空间查询XElement

[英]How to query XElement with two namespaces

I'm trying to find the inner text value of an element using LINQ-to-XML (an XElement object). 我正在尝试使用LINQ-to-XML(一个XElement对象)找到元素的内部文本值。 I make my service call and get an XML response back that I've successfully loaded into an XElement object. 我进行了服务调用并获得了一个XML响应,我已成功加载到XElement对象中。 I want to extract the inner text of one of the elements - however, every time I try to do this, I get a null result. 我想提取其中一个元素的内部文本 - 但是,每次我尝试这样做时,我得到一个null结果。

I feel like I'm missing something super-simple, but I'm fairly new to LINQ-to-XML. 我觉得我错过了一些超级简单的东西,但我对LINQ-to-XML相当新。 Any help is appreciated. 任何帮助表示赞赏。

I'm trying to get the inner text value of the StatusInfo/Status element. 我正在尝试获取StatusInfo / Status元素的内部文本值。 Here's my XML document that's returned: 这是我返回的XML文档:

<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">My Response</title>
  <id>tag:foo.com,2012:/bar/06468dfc-32f7-4650-b765-608f2b852f22</id>
  <author>
    <name>My Web Services</name>
  </author>
  <link rel="self" type="application/atom+xml" href="http://myServer/service.svc/myPath" />
  <generator uri="http://myServer" version="1">My Web Services</generator>
  <entry>
    <id>tag:foo.com,2012:/my-web-services</id>
    <title type="text" />
    <updated>2012-06-27T14:22:42Z</updated>
    <category term="tag:foo.com,2008/my/schemas#system" scheme="tag:foo.com,2008/my/schemas#type" />
    <content type="application/vnd.my.webservices+xml">
      <StatusInfo xmlns="tag:foo.com,2008:/my/data">
        <Status>Available</Status>  <!-- I want the inner text -->
      </StatusInfo>
    </content>
  </entry>
</feed>

Here's a snippet of code that I'm using to extract the value (which doesn't work): 这是我用来提取值的代码片段(不起作用):

    XElement root = XElement.Load(responseReader);
    XNamespace tag = "tag:foo.com,2008:/my/data";
    var status = (from s in root.Elements(tag + "Status")
                 select s).FirstOrDefault();

My status variable is always null . 我的status变量始终为null I've tried several variations on this, but to no avail. 我已经尝试了几种变体,但无济于事。 The part that's confusing me is the namespace -- tag and 2008 are defined. 令我困惑的部分是命名空间 - tag2008定义。 I don't know if I'm handling this correctly or if there's a better way to deal with this. 我不知道我是否正确处理这个问题,或者是否有更好的方法来解决这个问题。

Also, I don't have control over the XML schema or the structure of the XML. 此外,我无法控制XML架构或XML的结构。 The service I'm using is out of my control. 我正在使用的服务不受我的控制。

Thanks for any help! 谢谢你的帮助!

Try Descendants() instead of Elements() : 尝试Descendants()而不是Elements()

XElement x = XElement.Load(responseReader);
XNamespace ns = "tag:foo.com,2008:/my/data";
var status = x.Descendants(ns + "Status").FirstOrDefault().Value;

There are 2 Namespaces in the feed: Feed中有2个命名空间:

  1. the Atom namespace Atom命名空间
  2. the tag namespace 标签名称空间

The outer xml needs to use the Atom namespace, while a portion of the inner xml needs to use the tag namespace. 外部xml需要使用Atom命名空间,而内部xml的一部分需要使用标记命名空间。 ie,

var doc = XDocument.Load(responseReader);
XNamespace nsAtom = "http://www.w3.org/2005/Atom";
XNamespace nsTag = "tag:foo.com,2008:/my/data";

// get all entry nodes / use the atom namespace
var entry = doc.Root.Elements(nsAtom + "entry");

// get all StatusInfo elements / use the atom namespace
var statusInfo = entry.Descendants(nsTag + "StatusInfo");

// get all Status / use the tag namespace
var status = statusInfo.Elements(nsTag + "Status");

// get value of all Status
var values = status.Select(x => x.Value.ToString()).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM