Xdocument,选择正确的节点

发布于 2024-10-01 06:18:15 字数 3010 浏览 0 评论 0原文

我正在尝试构建一个 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 子句,或者有更好的方法吗?

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-10-08 06:18:16
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>();
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>();
孤檠 2024-10-08 06:18:15

你可以尝试:

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

You could try:

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文