Xdocument,选择正确的节点 [英] Xdocument, picking the right nodes

查看:78
本文介绍了Xdocument,选择正确的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构造一个linq查询,以提取具有特定元素的所有节点.

I'm trying to construct a linq query that pulls all nodes that have a particular element.

在以下情况下,您会注意到第二个条目还有一些额外的元素:DisplayOnSignup,SortOrder等.

In the case below, you'll notice that the second entry has a few extra elements: DisplayOnSignup, SortOrder, etc.

我希望linq给我所有具有SortOrder元素的入口节点.

I'd like the linq to give me all entry nodes that have a SortOrder element.

xml文档如下所示:

The xml doc looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<feed >
    <entry>
        <link href="/ws/customers/testacct/lists/removed" rel="edit"></link>
        <id>http://api.constantcontact.com/ws/customers/testacct/lists/removed</id>
        <title type="text">Removed</title>
        <updated>2010-11-10T19:03:09.253Z</updated>
        <author>
            <name>Test</name>
        </author>
        <content type="application/vnd.ctct+xml">
            <ContactList id="http://api.constantcontact.com/ws/customers/testacct/lists/removed">
                <Name>Removed</Name>
                <ShortName>Removed</ShortName>
            </ContactList>
        </content>
    </entry>
    <entry>
        <link href="/ws/customers/testacct/lists/1" rel="edit"></link>
        <id>http://api.constantcontact.com/ws/customers/testacct/lists/1</id>
        <title type="text">General Interest</title>
        <updated>2010-11-10T19:03:09.253Z</updated>
        <author>
            <name>Constant Contact</name>
        </author>
        <content type="application/vnd.ctct+xml">
            <ContactList id="http://api.constantcontact.com/ws/customers/testacct/lists/1">
                <OptInDefault>true</OptInDefault>
                <Name>General Interest</Name>
                <ShortName>General Interest</ShortName>
                <DisplayOnSignup>Yes</DisplayOnSignup>
                <SortOrder>0</SortOrder>
                <Members id="http://api.constantcontact.com/ws/customers/testacct/lists/1/members"></Members>
                <ContactCount>3</ContactCount>
            </ContactList>
        </content>
    </entry>
</feed>

到目前为止,我的查询如下:

My query so far looks like:

XDocument loaded = XDocument.Parse(response);

result = (from entry in loaded.Descendants("entry")
      select new CcList {
          LinkHref = entry.Element("link").Attribute("href").Value,
          Id = entry.Element("id").Value,
          Title = entry.Element("title").Value,
          Updated = entry.Element("updated").Value,
          ListName = entry.Element("content").Element("ContactList").Element("Name").Value,
          OptInDefault = entry.Element("content").Element("ContactList").Element("OptInDefault").Value,
          ShortName = entry.Element("content").Element("ContactList").Element("ShortName").Value,
          SortOrder = entry.Element("content").Element("ContactList").Element("SortOrder").Value
      }).ToList<CcList>();

我应该把where子句放在哪里还是有更好的方法?

What do I put as the where clause OR is there a better way?

推荐答案

您可以尝试:

var result = (
    from entry in loaded.Descendants("entry")
    where entry.Descendants("SortOrder").Count() > 0
    select new CcList {
        LinkHref = entry.Element("link").Attribute("href").Value,
        Id = entry.Element("id").Value,
        Title = entry.Element("title").Value,
        Updated = entry.Element("updated").Value,
        ListName = entry.Element("content").Element("ContactList").Element("Name").Value,
        OptInDefault = entry.Element("content").Element("ContactList").Element("OptInDefault").Value,
        ShortName = entry.Element("content").Element("ContactList").Element("ShortName").Value,
        SortOrder = entry.Element("content").Element("ContactList").Element("SortOrder").Value
    }
).ToList<CcList>();

这篇关于Xdocument,选择正确的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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