c# - Xdocument,选择正确的节点

标签 c# linq-to-xml

我正在尝试构建一个 linq 查询来提取具有特定元素的所有节点。

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

我希望 linq 为我提供所有具有 SortOrder 元素的入口节点。

xml 文档如下所示:

<?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>

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

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 子句,或者有更好的方法吗?

最佳答案

你可以试试:

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>();

关于c# - Xdocument,选择正确的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4148223/

相关文章:

c# - 在 ASP.NET 中使用 SecureString 有什么好处吗?

c# - ASP.Net 中非 ANSI 字符的 QueryString 编码

c# - 我可以更改第三方 dll 中的 dll 引用吗?

c# - 在 C# 中,有没有一种方法可以使用短前缀而不是每个节点的完整命名空间来生成 XDocument?

c# - 多元素到多行

c# - 为什么即使 GetLastError 为 0,CLRHosting API 也无法正常工作?

c# - 对象到字节的转换

c# - LINQ to XML - 如何获取索引

xml - 使用 XDocument.Load 的多线程

c# - 如何迭代到下一个同级 xml 元素?