0
我有下面的代碼,我試圖刪除所有傳遞的html元素。使用C#4.0中的XmlDocument刪除HTML標記
String inputString = "<img class="imgRight" title="Zürich, Switzerland" src="test.png" alt="Switzerland" width="44" height="44"/>
<p class="first">Zurich</p>
<p class="second">Test</p>
<p class="first">Testing</p>
<img class="imgRight" title="Zürich, Switzerland" src="1.png" alt="Switzerland" width="44" height="44"/>
<a href="test.aspx">Hello</a>"; //Sample HTML String
String[] htmlTags = new String[] { "a", "img", "link:ComponentLink" };
String removedTagsHtml = RemoveHTMLTags(inputString,htmlTags);//Giving error "There are multiple root elements."
public static string RemoveHTMLTags(String inputString, String[] htmlTags)
{
String strResult = String.Empty;
foreach (String htmlTag in htmlTags)
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(inputString);
XmlNamespaceManager xMan = new XmlNamespaceManager(xDoc.NameTable);
xMan.AddNamespace("xs", xDoc.DocumentElement.NamespaceURI);
XmlNode xNode = xDoc.SelectSingleNode("xs:" + htmlTag + "", xMan);
xDoc.RemoveAll();
xDoc.AppendChild(xNode);
string seeOutputHere = xDoc.OuterXml;
}
return strResult;
}
函數產生錯誤「有多個根元素。」
HtmlAgilityPack將是最後一次嘗試,如果我未能得到所需的結果,任何想法如何解決上述問題 – 2013-05-06 05:16:30
請參閱我的鏈接到另一個問題,描述如何從多根字符串加載一組'XElement'。 – 2013-05-06 05:27:47