2010-11-10 27 views
1

我想構建一個linq查詢,拉動所有具有特定元素的節點。Xdocument,挑選正確的節點

在下面的情況下,您會發現,第二個條目具有一些新的元素: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子句或有沒有更好的辦法?

回答

3

你可以嘗試:

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

如果我想補充的所有XML節點通過node.Name一個哈希表什麼? – Achilleterzo 2011-03-15 10:40:45

3
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>(); 
+0

+1:你的工作也是如此。弗雷德裏克只是快了一分鐘..謝謝, – NotMe 2010-11-10 19:39:59

+0

@Chris Lively:難道不是這樣:)很高興幫助。 – Sorax 2010-11-10 19:42:23