2013-01-14 38 views
0

我有下面的XML:獲取元素集合從XML

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <author> 
     <name /> 
    </author> 
    <title>Publishing Point Collection</title> 
    <updated>2013-01-14T15:58:44.589Z</updated> 
    <entry> 
     <id>http://example.com:80/test filename.isml/settings</id> 
     <title>Test Title</title> 
     <updated>2013-01-14T15:47:15Z</updated> 
     <link href="http://example.com:80/test filename.isml/settings" rel="related" type="application/atom+xml" title="Settings" /> 
     <link href="http://example.com:80/test filename.isml/state" rel="related" type="application/atom+xml" title="State" /> 
     <link href="http://example.com:80/test filename.isml/statistics" rel="related" type="application/atom+xml" title="Statistics" /> 
    </entry> 
</feed> 

我做加載文檔:

//xmlContent is the XML shown above. 
XDocument publishingDocument = XDocument.Parse(xmlContent); 

我試圖讓所有的鏈接元素(特別是href屬性,但是,現在只是元素)。但是,任何時候我指定一個XName我永遠不會得到任何結果。我試過了:

var data = publishingDocument.Elements("entry"); 
var data = publishingDocument.Elements("link").Attributes("href"); 
var data = publishingDocument.Root.Elements("entry"); 

這些都沒有返回任何數據。我必須錯過一些非常簡單的事情。

+0

嘗試VAR數據= publishingDocument.Descendants( 「鏈接」)。元素(),我相信,只適用於根級元素。 – Pete

+0

@Pete - 試過了。我沒有得到任何結果。 –

回答

2

嘗試

XNamespace ns = "http://www.w3.org/2005/Atom"; 
var data = publishingDocument.Descendants(ns + "entry"); 
+0

完美運作。乾杯! –