2012-07-19 88 views
0

我從Gmail中的XML提要內。因而提取屬性值:使用LINQ to XML - 如何元素

<feed version="0.3"> 
    <title>Gmail - Inbox for [email protected]</title> 
    <tagline>New messages in your Gmail Inbox</tagline> 
    <fullcount>9</fullcount> 
    <link rel="alternate" href="http://mail.google.com/mail" type="text/html"/> 
    <modified>2012-07-19T02:43:55Z</modified> 
    <entry> 
     <title>$50 or $100 Nightly Resort Credit and Room Upgrade</title> 
     <summary>Add to Address Book | Update email address Marriott® FIND A HOTEL EXPLORE PLAN MARRIOTT REWARDS</summary> 
     <link rel="alternate" href="http://.google.com" type="text/html"/><modified>2012-07-19T01:23:00Z</modified> 
     <issued>2012-07-19T01:23:00Z</issued> 
     <id>tag:gmail.google.com,2004:1407882080256681207</id> 
     <author> 
      <name>Marriott</name> 
      <email>[email protected]</email> 
     </author> 
    </entry> 
    <entry> 
     <title>Tim Anderson's ITWriting » Blog Archive Resort » Moving a database from on-premise SQL Server to SQL Azure: some hassle</title> 
     <summary>Tim Anderson's ITWriting Tech writing blog Home Articles Reviews Gadget Writing Forthcoming</summary> 
     <link rel="alternate" href="http://google.comen" type="text/html"/><modified>2012-07-19T00:55:25Z</modified> 
     <issued>2012-07-19T00:55:25Z</issued> 
     <id>tag:gmail.google.com,2004:1407880344364808586</id> 
     <author> 
      <name>me</name> 
      <email>[email protected]</email> 
     </author> 
    </entry>   

我創建了一個LinqPad查詢這樣:

XDocument xmlDoc = XDocument.Load(@"C:\Users\Brad Agee\Documents\feeds.xml"); 

var q = from c in xmlDoc.Descendants("entry") 
where c.Element("title").Value.ToLower().Contains("resort") 
select new { 
name = c.Element("title").Value, 
url = ??, 
email ?? 
}; 

q.Dump(); 

我怎樣才能提取link標籤href屬性爲url屬性的值和email屬性的值到email屬性?

回答

1

好吧。 Href是元素鏈接的屬性。 所以

c.Element("link").Attribute("href") 

電子郵件是筆者元素的元素

所以

c.Element("auhtor").Element("email").Value 

終於

select new 
{ 
    name = c.Element("title").Value, 
    url = c.Element("link").Attribute("href").Value, 
    email = c.Element("author").Element("email").Value 
}; 
+0

如此簡單,當你看到它... – blub 2012-07-19 14:19:46

+0

謝謝您幫幫我! – blub 2012-07-19 14:20:26