Xdocument, picking the right nodes

安稳与你 提交于 2019-12-11 09:27:39

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/4148223/xdocument-picking-the-right-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!