2012-07-25 132 views
0

我需要xml文件重複項。裏面的「項目」我有這樣的標題,pubdate的,說明,直流場:創造者與repeatwp:評論...請參閱下面的xml文件..使用xdocument讀取重複元素xml

<channel> 
    <item> 
     <title>What Messed With My Head: Summit 2011</title> 
     <link>http://www.wcablog.com/2011/08/what-messed-with-my-head-summit-2011/</link> 
     <pubDate>Fri, 26 Aug 2011 09:10:04 +0000</pubDate> 
     <dc:creator>willowcreekassociation</dc:creator> 
     <guid isPermaLink="false">http://www.wcablog.com/?p=1706</guid> 
     <description></description> 
     <content:encoded> 
      <![CDATA[text here]]> 
     </content:encoded> 
     <wp:comment> 
      <wp:comment_id>1016</wp:comment_id> 
      <wp:comment_author><![CDATA[]]></wp:comment_author> 
      <wp:comment_author_email>[email protected]</wp:comment_author_email> 
      <wp:comment_author_url></wp:comment_author_url> 
      <wp:comment_author_IP></wp:comment_author_IP> 
      <wp:comment_date>2011-08-26 20:13:00</wp:comment_date> 
      <wp:comment_content><![CDATA[some text ]]></wp:comment_content> 
     </wp:comment> 
     <wp:comment> 
      <wp:comment_id>1016</wp:comment_id> 
      <wp:comment_author><![CDATA[]]></wp:comment_author> 
      <wp:comment_author_email>[email protected]</wp:comment_author_email> 
      <wp:comment_author_url></wp:comment_author_url> 
      <wp:comment_author_IP></wp:comment_author_IP> 
      <wp:comment_date>2011-08-26 20:13:00</wp:comment_date> 
      <wp:comment_content><![CDATA[some text ]]></wp:comment_content> 
     </wp:comment> 
    </item> 
    <item> 
     <title>What Messed With My Head: Summit 2011</title> 
     <link>http://www.wcablog.com/2011/08/what-messed-with-my-head-summit-2011/</link> 
     <pubDate>Fri, 26 Aug 2011 09:10:04 +0000</pubDate> 
     <dc:creator>willowcreekassociation</dc:creator> 
     <guid isPermaLink="false">http://www.wcablog.com/?p=1706</guid> 
     <description></description> 
     <content:encoded> 
      <![CDATA[text here]]> 
     </content:encoded> 
    </item> 
    <item> 
     <title>What Messed With My Head: Summit 2011</title> 
     <link>http://www.wcablog.com/2011/08/what-messed-with-my-head-summit-2011/</link> 
     <pubDate>Fri, 26 Aug 2011 09:10:04 +0000</pubDate> 
     <dc:creator>willowcreekassociation</dc:creator> 
     <guid isPermaLink="false">http://www.wcablog.com/?p=1706</guid> 
     <description></description> 
     <content:encoded> 
      <![CDATA[text here]]> 
     </content:encoded> 
     <wp:comment></wp:comment> 
     <wp:comment></wp:comment> 
    </item> 
</channel> 

我使用下面的代碼閱讀XML ..

XDocument xDoc = XDocument.Load("willowcreekassociationblog.wordpress.xml"); 

     var list = xDoc.Descendants("Occurrence") 
     .Select(o => new List.XMLList 
     { 
      title = (string)o.Element("title"), 
      URL = (string)o.Element("link"), 
      Descr = (string)o.Element("Description"), 
      StartDate = (DateTime)o.Element("pubdate"), 
     }) 
     .ToList(); 

但我不知道如何讀的WP:我上面的代碼註釋...有人能幫助我如何做到這一點?

回答

0

你還沒有說過你想對數據做什麼,這使得問題很難回答。您還沒有表現出wp命名空間別名定義在哪裏,但嘿...

你想要的東西這個,你Select調用內:

Comments = o.Elements(wp + "comment") 
      .Select(comment => Comment.FromXElement(comment)) 
      .ToList(); 

我發現它有助於給靜可以執行從XElement轉換的域對象中的方法 - 它使事情更清晰。

FromXElement那麼會是這樣的:

public static Comment FromXElement(XElement x) 
{ 
    return new Comment((int) x.Element(wp + "comment_id"), 
         (string) x.Element(wp + "author"), 
         ...); 
} 
+0

我只需要讀取數據並將其分配給一個list..and將數據保存到數據庫.. – Srav 2012-07-25 20:33:03

+0

@Srav:嗯,我假設你已經有一個類型來表示評論,就像你有一個類型來表示該項目。 – 2012-07-25 20:38:01