我有下面的XML作爲一個IQueryable傳遞給方法,即的LINQ to XML,更換子節點,但保持狀態
XML:
<body>
<p>Some Text</p>
<p>Some Text</p>
<pullquote>This is pullquote</pullquote>
<p>Some Text</p>
<p>Some Text</p>
<body>
方法:
public static string CreateSection(IQueryable<XElement> section)
{
var bodySection = from b in articleSection.Descendants("body")
select b;
//Do replacement here.
var bodyElements = bodySection.Descendants();
StringBuilder paragraphBuilder = new StringBuilder();
foreach (XElement paragraph in bodyElements)
{
paragraphBuilder.Append(paragraph.ToString());
}
return paragraphBuilder.ToString();
}
我想完成的是將<pullquote>
替換爲<p>
(也可能是添加屬性)。
我的問題不是實際的替換(XElement.ReplaceWith()),而是替換之後的更改並不反映StringBuilder使用的bodySection變量。
我該如何去解決這個問題?
你是對的,使用整個集合列表似乎是唯一的解決方案 –
2010-02-25 06:31:54
@BK:嚴格地說,它不一定是'List' - 任何實現查詢並保持結果的東西都可以。實際存儲是一個實現細節 - 但列表是最簡單的方法。 –
2010-02-25 07:07:34