简体   繁体   English

在C#中写入XML时,停止输出CDATA标记转义

[英]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 &lt; 但是,尖括号被转换为&lt; and &gt; &gt;

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 + "]]>";

转换XML <!--CDATA to DataTable C#</div--><div id="text_translate"><p> 我有 Webservice SOAP 方法,它将 Json 作为输入参数。</p><pre> //For sample string jsonString = "[{"Year":"2020","UserId":"1","Comp":"20","DeptId":"32","CategoryId":"53","ItemId":"0"}]"; string[] processString = budget.BudgetTagNet(jsonString);</pre><p> 现在“budget.BudgetTagNet(jsonString)”返回前面提到的字符串数组,并且 SOAP 响应显示以下值: <a href="https://i.stack.imgur.com/ms2hp.png" rel="nofollow noreferrer">SOAP 响应结果</a></p><p>现在我想将返回的预算数组转换为 DataTable 以进一步用于我的项目,但我做不到。 实现这一目标的最佳方法是什么。 我尝试过的方法如下所述。</p><pre> string jsonString = CommonEnum.DataTableToJSON(Table); err.Message = "Json String is: " + jsonString; err.Insert(); string[] processString = budget.BudgetTagNet(jsonString); err.Message = "Budget service data is: " + processString[1]; err.Insert(); var XMLAsString = CommonEnum.SerializeXML(processString[1]); err.Message = "Converted XML To String data is: " + JsonConvert.SerializeObject(XMLAsString); err.Insert(); dt = (DataTable) JsonConvert.DeserializeObject(XMLAsString, (typeof(DataTable))); err.Message = "DataTable data: " + JsonConvert.SerializeObject(dt); err.Insert();</pre><p> 最后插入数据库中的数据,如上述方法是:<a href="https://i.stack.imgur.com/zSY2Q.png" rel="nofollow noreferrer">插入的数据库值</a></p></div> - Convert XML <!CDATA to DataTable C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 中的 XML CDATA 读取 - XML CDATA reading in C# 具有cdata的转义xml,同时也具有转义数据值和标记 - escaped xml with cdata that has also has both escaped data value and tags 使用C#的XML中的CDATA - CDATA in XML using C# 从文件读取时,C#双引号路径名被转义 - C# double-quoted path name being escaped when read from file 从C#写入xml - Writing to xml from C# 从包含HTML标签的xml中读取CDATA - read CDATA from xml containing HTML tags C#:xml CData中的字节数组 - C#: byte array in xml CData 转换XML <!--CDATA to DataTable C#</div--><div id="text_translate"><p> 我有 Webservice SOAP 方法,它将 Json 作为输入参数。</p><pre> //For sample string jsonString = "[{"Year":"2020","UserId":"1","Comp":"20","DeptId":"32","CategoryId":"53","ItemId":"0"}]"; string[] processString = budget.BudgetTagNet(jsonString);</pre><p> 现在“budget.BudgetTagNet(jsonString)”返回前面提到的字符串数组,并且 SOAP 响应显示以下值: <a href="https://i.stack.imgur.com/ms2hp.png" rel="nofollow noreferrer">SOAP 响应结果</a></p><p>现在我想将返回的预算数组转换为 DataTable 以进一步用于我的项目,但我做不到。 实现这一目标的最佳方法是什么。 我尝试过的方法如下所述。</p><pre> string jsonString = CommonEnum.DataTableToJSON(Table); err.Message = "Json String is: " + jsonString; err.Insert(); string[] processString = budget.BudgetTagNet(jsonString); err.Message = "Budget service data is: " + processString[1]; err.Insert(); var XMLAsString = CommonEnum.SerializeXML(processString[1]); err.Message = "Converted XML To String data is: " + JsonConvert.SerializeObject(XMLAsString); err.Insert(); dt = (DataTable) JsonConvert.DeserializeObject(XMLAsString, (typeof(DataTable))); err.Message = "DataTable data: " + JsonConvert.SerializeObject(dt); err.Insert();</pre><p> 最后插入数据库中的数据,如上述方法是:<a href="https://i.stack.imgur.com/zSY2Q.png" rel="nofollow noreferrer">插入的数据库值</a></p></div> - Convert XML <!CDATA to DataTable C# 在C#中将Saxon的XdmNode写入流时如何包含CDATA - How to include CDATA when writing Saxon's XdmNode to Stream in C# 编写xml并使用xample xml输出将其读回c# - Writing xml and reading it back c# with xample xml output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM