0

我有一个来自 Gmail 的 xml 提要:

<feed version="0.3">
    <title>Gmail - Inbox for bagee18@gmail.com</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>Marriott@marriott-email.com</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>bagee18@gmail.com</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();

如何将标签的属性值提取到href属性中,并将属性值提取到属性中?linkurlemailemail

4

1 回答 1

1

好。Href 是元素链接的一个属性。所以

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

email 是元素作者的一个元素

所以

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 
};
于 2012-07-19T12:06:36.880 回答