[英]Stop CDATA tags from being output-escaped when writing to XML in C#
We're creating a system outputting some data to an XML schema. 我们正在创建一个将一些数据输出到XML模式的系统。 Some of the fields in this schema need their formatting preserved, as it will be parsed by the end system into potentially a Word doc layout.
此架构中的某些字段需要保留其格式,因为最终系统会将其解析为潜在的Word文档布局。 To do this we're using
<![CDATA[Some formatted text]]>
tags inside of the App.Config file, then putting that into an appropriate property field in a xsd.exe generated class from our schema. 为此,我们在App.Config文件中使用
<![CDATA[Some formatted text]]>
标记,然后将其放入xsd.exe从架构中生成的类中的适当属性字段。 Ideally the formatting wouldn't be out problem, but unfortunately thats just how the system is going. 理想情况下,格式化不会出现问题,但是不幸的是,这只是系统的运行方式。
The App.Config section looks as follows: App.Config部分如下所示:
<header>
<![CDATA[Some sample formatted data]]>
</header>
The data assignment looks as follows: 数据分配如下所示:
HeaderSection header = ConfigurationManager.GetSection("header") as HeaderSection;
report.header = "<[CDATA[" + header.Header + "]]>";
Finally, the Xml output is handled as follows: 最后,Xml输出的处理如下:
xs = new XmlSerializer(typeof(report));
fs = new FileStream (reportLocation, FileMode.Create);
xs.Serialize(fs, report);
fs.Flush();
fs.Close();
This should in theory produce in the final Xml a section that has information with CDATA tags around it. 从理论上讲,这应该在最终的Xml中产生一个部分,该部分周围包含带有CDATA标签的信息。 However, the angled brackets are being converted into
<
但是,尖括号被转换为
<
and >
和
>
I've looked at ways of disabling Outout Escaping, but so far can only find references to XSLT sheets. 我已经研究了禁用Outs Escaping的方法,但是到目前为止只能找到对XSLT工作表的引用。 I've also tried
@"<[CDATA["
with the strings, but again no luck. 我也尝试了
@"<[CDATA["
和字符串,但是再次没有运气。
Any help would be appreciated! 任何帮助,将不胜感激!
You're confusing markup with content. 您将标记与内容混淆了。
When you assign the string "<![CDATA[ ... ]]>"
to the value, you are saying that is the content that you wish to put in there. 当您指定字符串
"<![CDATA[ ... ]]>"
的值,你说的是,你要摆在那里的内容 。 The XmlSerializer
does not, and indeed should not, attempt to infer any markup semantics from this content, and simply escapes it according to the normal rules. XmlSerializer
不会(实际上也不应该)尝试从该内容中推断出任何标记语义,而只是按照常规规则对其进行转义。
If you want CDATA markup in there, then you need to explicitly instruct the serializer to do so. 如果要在其中添加CDATA 标记 ,则需要明确指示串行器这样做。 Some examples of how to do this are here .
如何执行此操作的一些示例在此处 。
Have you tried changing 您是否尝试过改变
report.header = "<[CDATA[" + header.Header + "]]>";
to 至
report.header = "<![CDATA[" + header.Header + "]]>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.