C# XML 解析:读取 CDATA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12490637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:25:52  来源:igfitidea点击:

XML parsing : Reading CDATA

c#windows-phone-7rssxml-parsingcdata

提问by E.Mert

<item><title>this is title</title><guid isPermaLink="true">http://www.i.com/video/nokia-lumia-920-deki-pureview_2879.html</guid><link>http://www.i.com/video/nokia-lumia-920-deki-pureview_2879.html</link>
<description><![CDATA[this is the info.]]></description>
<pubDate>Wed, 5 Sep 2012 22:10:00 UT</pubDate>
<media:content type="image/jpg" expression="sample" fileSize="2956" medium="image" url="http://media.chip.com.tr/images/content/video/88/201209060102428081-0.jpg"/>
<enclosure type="image/jpg" url="http://media.chip.com.tr/images/content/video/88/201209060102428081-0.jpg" length="2956"/></item>

I want read the CDATA in <"description">

我想阅读 <"description"> 中的 CDATA

I wrote this

我写了这个

        var x = e.Result;// e is downlaoded xml file

        var videos = XElement.Parse(e.Result);

        var fList = (from haber in videos.Descendants("channel").Elements("item")
                     select new Video
                     {
                         title = haber.Element("title").Value,
                         link = haber.Element("link").Value,
                         //description = ???????
                     }).ToList();

what should i write to description ? //EDIT Answer: The same way

我应该写什么来描述?//编辑答案:同样的方式

but if the description like this?

但如果是这样的描述呢?

<![CDATA[<p>Zombiler ad?na ne umduk ne bulduk!</p> <p> </p><p><img style="margin: 5px 0px 5px 5px; border: 1px solid #333333; float: right;" alt="Black_ops" src="http://or.com/images/stories/haber/haberler6/20120918_Castlevania/Black_ops.jpg" height="0" width="0" /><strong>Black Ops 2</strong>'de Zombi modu olabilir haberi ??kt???ndan beri bir ses, bir g?rüntü beklerken <strong>Call of Duty</strong>'nin resmi <strong>Youtube</strong> sayfas?nda a?a??daki video yay?nland?. A??k?as? ne demek istiyorlar anlamak gü?. <p><a href="http://or.com/haberler/1-yeni-oyunlar/10624-esi-gorulmemis-call-of-duty-videosu">Devam?n? oku...</a></p>]]>

采纳答案by Jon Skeet

You should be able to use exactly the same code:

您应该能够使用完全相同的代码:

description = haber.Element("description").Value

Or

或者

description = (string) haber.Element("description")

LINQ to XML will take care of reading the text for you.

LINQ to XML 将负责为您阅读文本。

回答by Pedro Lamas

To read the CDATA block you just use the same methods; you what you want is to clean the HTML from it, then check this answer.

要读取 CDATA 块,您只需使用相同的方法;您想要的是从中清除 HTML,然后检查此答案