首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xdocument,选择正确的节点

Xdocument,选择正确的节点
EN

Stack Overflow用户
提问于 2010-11-11 03:23:19
回答 2查看 480关注 0票数 1

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

在下面的例子中,您将注意到第二个条目有一些额外的元素: DisplayOnSignup、SortOrder等。

我想让linq给我所有有SortOrder元素的入口节点。

xml文档如下所示:

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
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子句,或者有更好的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-11 03:36:25

您可以尝试:

代码语言:javascript
复制
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>();
票数 3
EN

Stack Overflow用户

发布于 2010-11-11 03:37:53

代码语言:javascript
复制
XDocument loaded = XDocument.Parse(response);

var result = (
          from entry in loaded.Descendants("entry")
          where entry.Descendants().Any(x => x.Name == "SortOrder")
          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>();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4148223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档