<Results>
<ResultSet>"nothing special" Description="More of nothing"
<Results>
<Result>
<Body>Four in this group</Body>
<Body2>this is more stuff I want</Body2>
<Body3>This is interesting stuff I want</Body3>
<Body4>this is more stuff I want</Body4>
</Result>
<Result>
<Something1>Only 3 in the group</Something1>
<ID>this is more stuff I want</ID>
<Stuff>This is interesting stuff I want</Stuff>
</Result>
<Result>
<Tag1>Only 3 in the group</Tag1>
<Tag2>this is more stuff I want</Tag2>
<Tag3>This is interesting stuff I want</Tag3>
</Result>
</Results>
</ResultSet>
</Results>
我該如何使用Linq到XML來取出所有的<Result>
塊?正如你可以看到每個塊可以有任何數量的元素,我不知道他們被稱爲什麼。我的最終目標是將這些塊封裝到對象中,然後再傳遞給我的應用程序。LINQ To XML - 如何查詢內部元素中的所有元素?
我遇到的問題是我不能讓每個組使用Linq分開打破。我能得到的最接近的結果是將所有<Result>
組中的所有元素都歸爲一個大列表(使用後代)。
編輯:這是我用來恢復每個<Result>
組中的所有內容的代碼。我無法弄清楚如何迭代這個查詢中的列表來分解名稱/值對。我不知道元素的名稱,所以我不能通過名稱來引用它們。最後,我只想每個<Result>
中的每個元素的名稱/值對。
var query = from item in xml.Descendants("Result")
select item;
任何幫助?
謝謝,斯科特
'Descendants'不應返回 「內部」 的事情'結果'元素。它應該返回一系列Result元素,這顯然是你需要的。 「Descendants」不適用什麼? – 2010-11-19 05:11:36
我剛更新了帖子以顯示我正在使用的代碼以及我正在嘗試執行的操作。 – Scott 2010-11-19 05:44:52