开发者

Get innerText from XElement

Hey guys, I have an XML file I need to parse, but only for the text, not the HTML. Here's an example of a node:

<highlights><![CDATA[<ul style="color:#000000;font-size:small;font-family:verdana,geneva,sans-serif"><li>Classy &amp; elegant purse hooks</li><li>Choose from various styles<br></li><li>Stable and reliable</li><li>Makes a great gift!  </li></ul>  ]]></highlights> 

As you can see, the CDATA has HTML code in it, so when I do element.Value I also get the tags. Is there any 开发者_如何学编程simple way to get just the text?

Thank you!


Since the data in CDATA is not valid XML, you can use HtmlAgilityPack to parse the result:

XDocument xdoc = XDocument.Load("test.xml");
var html = xdoc.Descendants("highlights").First().Value;

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var result = htmlDoc.DocumentNode.InnerText;

Output:

Classy & elegant purse hooksChoose from various stylesStable and reliableMakes a great gift!


<literal id="continer"  runat="server"/>

continer.text=@"<highlights><![CDATA[<ul style="color:#000000;font-size:small;font-family:verdana,geneva,sans-serif"><li>Classy &amp; elegant purse hooks</li><li>Choose from various styles<br></li><li>Stable and reliable</li><li>Makes a great gift!  </li></ul>  ]]></highlights> ";

or

<div id="continer" runat="server"/>

continer.innerHtml==@"<highlights><![CDATA[<ul style="color:#000000;font-size:small;font-family:verdana,geneva,sans-serif"><li>Classy &amp; elegant purse hooks</li><li>Choose from various styles<br></li><li>Stable and reliable</li><li>Makes a great gift!  </li></ul>  ]]></highlights> ";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜