我讀過此網站,此問題之前詢問。但我試過的代碼,但它不工作HTMLAgilityPack - 刪除節點,但保留其值
所有的答案都是一樣的。使用單一的代碼
item.ParentNode.RemoveChild(item, true);
但我用我的代碼,但它不工作。我收到一個錯誤
Collection was modified; enumeration operation may not execute.
這是我的代碼截至目前
try
{
string html = "<xml><span> some text </span><p><span> Hello</span> </p></xml>";
var htmlSpan = new HtmlDocument();
htmlSpan.LoadHtml(html);
var rootSpan = htmlSpan.DocumentNode;
var anchorsSpan = rootSpan.Descendants("span");
foreach (var item in anchorsSpan)
{
item.ParentNode.RemoveChild(item, true);
}
//htmlSpan.Save(fileName);
Console.WriteLine(htmlSpan.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
我在做什麼是消除跨度標籤,但保留是文本
所以輸出必須
<xml> some text <p> Hello </p></xml>
看看[刪除不需要的標籤而不刪除內容](http://stackoverflow.com/a/12836974/3796048) –