解码 C# 中的 CDATA 部分

发布于 2024-07-30 20:15:04 字数 505 浏览 3 评论 0原文

我有一些 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 formatted
instead 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧竹 2024-08-06 20:15:04

其实我觉得很简单。 CDATA 部分将像另一个 XmlNode 一样加载到 XmlDocument 中,区别在于该节点将具有属性 NodeType = CDATA,这意味着如果您有 XmlNode node = doc.SelectSingleNode("section/description"); 该节点将有一个带有 InnerTextChildNode > 属性填充了纯数据,如果您想删除特殊字符,只需使用 Trim() 即可获得数据。

代码看起来像

XmlNode cDataNode = doc.SelectSingleNode("section/description").ChildNodes[0];
string finalData = cDataNode.InnerText.Trim();

谢谢
XOnDaRocks

Actually i think is pretty much simple. the CDATA section it will be loaded in the XmlDocument like another XmlNode the difference is that this node is going to has the property NodeType = CDATA, wich it mean if you have the XmlNode node = doc.SelectSingleNode("section/description"); that node will have a ChildNode with the InnerText property filled the pure data, and there is you want to remove the especial characters just use Trim() and you will have the data.

The code will look like

XmlNode cDataNode = doc.SelectSingleNode("section/description").ChildNodes[0];
string finalData = cDataNode.InnerText.Trim();

Thanks
XOnDaRocks

一桥轻雨一伞开 2024-08-06 20:15:04

@Franky 的解决方案的更简单形式:

doc.SelectSingleNode("section/description").FirstChild.Value

Value 属性相当于 转换的 Data 属性XmlCDataSection 类型。

A simpler form of @Franky's solution:

doc.SelectSingleNode("section/description").FirstChild.Value

The Value property is equivalent to the Data property of the casted XmlCDataSection type.

十六岁半 2024-08-06 20:15:04

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.

慕烟庭风 2024-08-06 20:15:04

我认为最好的办法是...

XmlCDataSection cDataNode = (XmlCDataSection)(doc.SelectSingleNode("section/description").ChildNodes[0]);

string finalData = cDataNode.Data;

I think the best way is...

XmlCDataSection cDataNode = (XmlCDataSection)(doc.SelectSingleNode("section/description").ChildNodes[0]);

string finalData = cDataNode.Data;
泼猴你往哪里跑 2024-08-06 20:15:04

您可以使用 Linq 读取 CDATA。

XDocument xdoc = XDocument.Load("YourXml.xml");
xDoc.DescendantNodes().OfType<XCData>().Count();

通过这种方式很容易获得 Value。

以下是 MSDN 上的一个很好的概述: http://msdn.microsoft.com/en- us/library/bb308960.aspx

对于 .NET 2.0,您可能只需通过 Regex 传递它:

     string xml = @"<section>
                      <description>
                        <![CDATA[
                          This is a ""description""
                          that I have formatted
                        ]]>
                      </description>
                    </section>";

        XPathDocument xDoc = new XPathDocument(new StringReader(xml.Trim()));
        XPathNavigator nav = xDoc.CreateNavigator();
        XPathNavigator descriptionNode = 
            nav.SelectSingleNode("/section/description");

        string desiredValue = 
            Regex.Replace(descriptionNode.Value
                                     .Replace(Environment.NewLine, String.Empty)
                                     .Trim(),
                @"\s+", " ");

它会修剪您的节点值,用空替换换行符,并用 1 个空格替换 1 个以上的空格。 考虑到 CDATA 返回重要的空格,我认为没有其他方法可以做到这一点。

You can use Linq to read CDATA.

XDocument xdoc = XDocument.Load("YourXml.xml");
xDoc.DescendantNodes().OfType<XCData>().Count();

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:

     string xml = @"<section>
                      <description>
                        <![CDATA[
                          This is a ""description""
                          that I have formatted
                        ]]>
                      </description>
                    </section>";

        XPathDocument xDoc = new XPathDocument(new StringReader(xml.Trim()));
        XPathNavigator nav = xDoc.CreateNavigator();
        XPathNavigator descriptionNode = 
            nav.SelectSingleNode("/section/description");

        string desiredValue = 
            Regex.Replace(descriptionNode.Value
                                     .Replace(Environment.NewLine, String.Empty)
                                     .Trim(),
                @"\s+", " ");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文