解码 C# 中的 CDATA 部分
我有一些 XML,如下所示:
<section>
<description>
<![CDATA[
This is a "description"
that I have formatted
]]>
</description>
</section>
我使用 curXmlNode.SelectSingleNode("description").InnerText 访问它,但值返回
\r\n This is a "description"\r\n that I have formattedinstead of
This is a "description" that I have formatted.
有没有一种简单的方法可以从 CDATA 部分获取此类输出? 保留实际的 CDATA 标记似乎让它以相同的方式返回。
I have a bit of XML as follows:
<section>
<description>
<![CDATA[
This is a "description"
that I have formatted
]]>
</description>
</section>
I'm accessing it using curXmlNode.SelectSingleNode("description").InnerText
but the value returns
\r\n This is a "description"\r\n that I have formatted
instead of
This is a "description" that I have formatted.
Is there a simple way to get that sort of output from a CDATA section? Leaving the actual CDATA tag out seems to have it return the same way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
其实我觉得很简单。
CDATA
部分将像另一个XmlNode
一样加载到XmlDocument
中,区别在于该节点将具有属性 NodeType = CDATA,这意味着如果您有XmlNode node = doc.SelectSingleNode("section/description");
该节点将有一个带有InnerText
的ChildNode
> 属性填充了纯数据,如果您想删除特殊字符,只需使用Trim()
即可获得数据。代码看起来像
谢谢
XOnDaRocks
Actually i think is pretty much simple. the
CDATA
section it will be loaded in theXmlDocument
like anotherXmlNode
the difference is that this node is going to has the property NodeType = CDATA, wich it mean if you have theXmlNode node = doc.SelectSingleNode("section/description");
that node will have aChildNode
with theInnerText
property filled the pure data, and there is you want to remove the especial characters just useTrim()
and you will have the data.The code will look like
Thanks
XOnDaRocks
@Franky 的解决方案的更简单形式:
Value
属性相当于 转换的Data
属性XmlCDataSection
类型。A simpler form of @Franky's solution:
The
Value
property is equivalent to theData
property of the castedXmlCDataSection
type.CDATA 块实际上是逐字记录的。 根据 XML 规范的定义,CDATA 中的任何空格都是重要的。 因此,当您检索节点值时,您会得到该空格。 如果您想使用自己的规则删除它(因为 XML 规范没有指定删除 CDATA 中空格的任何标准方法),您必须自己执行此操作,使用
String.Replace
,根据需要 Regex.Replace
等。CDATA blocks are effectively verbatim. Any whitespace inside CDATA is significant, by definition, according to XML spec. Therefore, you get that whitespace when you retrieve the node value. If you want to strip it using your own rules (since XML spec doesn't specify any standard way of stripping whitespace in CDATA), you have to do it yourself, using
String.Replace
,Regex.Replace
etc as needed.我认为最好的办法是...
I think the best way is...
您可以使用 Linq 读取 CDATA。
通过这种方式很容易获得 Value。
以下是 MSDN 上的一个很好的概述: http://msdn.microsoft.com/en- us/library/bb308960.aspx
对于 .NET 2.0,您可能只需通过 Regex 传递它:
它会修剪您的节点值,用空替换换行符,并用 1 个空格替换 1 个以上的空格。 考虑到 CDATA 返回重要的空格,我认为没有其他方法可以做到这一点。
You can use Linq to read CDATA.
It's very easy to get the Value this way.
Here's a good overview on MSDN: http://msdn.microsoft.com/en-us/library/bb308960.aspx
for .NET 2.0, you probably just have to pass it through Regex:
that trims your node value, replaces newlines with empty, and replaces 1+ whitespaces with one space. I don't think there's any other way to do it, considering the CDATA is returning significant whitespace.