Legally use CDATA in XML

假如想象 提交于 2019-12-26 07:19:46

问题


I have an XML file which XML parser choke on. A part of it is :

<closedDeal><customer><![CDATA[ABC ]]></customer></closedDeal>

The error I got is

The literal string ']]>' is not allowed in element content. Error processing resource

What is the correct way of using CDATA? I need CDATA because the data is read from Excel, and could contain illegal character such as ALT+ENTER whitespace.

Please help. Thanks.


回答1:


What parser are you using? The sample you showed is definitely a valid XML. For example in .NET I successfully parsed this XML :

<?xml version="1.0" encoding="utf-8" ?>
<closedDeal>
  <customer><![CDATA[ABC ]]></customer>
</closedDeal>

using the following code:

using System;
using System.Xml.Linq;
using System.Xml.XPath;

public class Program
{
    static void Main(string[] args)
    {
        var doc = XElement.Load("test.xml");
        doc.XPathSelectElement("//customer");
        Console.WriteLine(doc.Value);
    }
}


来源:https://stackoverflow.com/questions/2468327/legally-use-cdata-in-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!