C#在CData中创建xml节点的副本(它本身在xml文件中)。 [英] C# Create duplicate of an xml node inside of CData (which itself in an xml file).

查看:62
本文介绍了C#在CData中创建xml节点的副本(它本身在xml文件中)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

<?xml version ='1.0'coding ='UTF-8'?>
< root>
< input>
<![CDATA [
< ServerLicence> abc< / ServerLicence>
< inputData>
< CreationPreferences>< / CreationPreferences>
< SelectObj1>
(?xml version =" 1.0" encoding =" UTF-8"?)(FILTER
CASESENSITIVE =" TRUE" DEPTH =" -1" RESULTSPERPAGE =" -1"
COMMAND =" QUERY")
(SELECT)
(/ SELECT)
(WHERE)
(条件)
(表达式)
(VALUE)
Replace_Placeholder
(VALUE)
(/ EXPRESSION)
(/ CONDITION)
(/ WHERE)
< / SelectObj1>
< / inputData>
]]>
< / input>
< / root>

我看到了一个xml文件,它有一些CData内容,也是xml格式。我需要替换Cdata中的文本 - "span style ="font-family:monospace; font-size:11.9925px; white-space:pre"> Replace_Placeholder"根据它收到的一些论据,在代码
中。因此,当有多个参数时,xml应如下所示:

<?xml version ='1.0'coding ='UTF-8 '?> 
< root>
< input>
<![CDATA [
< ServerLicence> abc< / ServerLicence>
< inputData>
< CreationPreferences>< / CreationPreferences>

< SelectObj1>
(?xml version =" 1.0" encoding =" UTF-8"?)(FILTER
CASESENSITIVE =" TRUE" DEPTH =" -1" RESULTSPERPAGE =" -1"
COMMAND =" QUERY")
(SELECT)
(/ SELECT)
(WHERE)
(条件)
(表达式)
(VALUE)
Replace_Placeholder
(VALUE)
(/ EXPRESSION)
(/ CONDITION)
(/ WHERE)
< / SelectObj1>

< SelectObj1>
(?xml version =" 1.0" encoding =" UTF-8"?)(FILTER
CASESENSITIVE =" TRUE" DEPTH =" -1" RESULTSPERPAGE =" -1"
COMMAND =" QUERY")
(SELECT)
(/ SELECT)
(WHERE)
(条件)
(表达式)
(VALUE)
Replace_Placeholder
(VALUE)
(/ EXPRESSION)
(/ CONDITION)
(/ WHERE)
< / SelectObj1>
< / inputData>
]]>
< / input>
< / root>

它应该再创建一个类型为"SelectObj1"的节点。并使用参数中的值替换  Replace_Placeholder中的文本。我被困在如何创建相同的节点并替换并最终保存xml!我希望我的问题很明确!


解决方案

您好, 


> CData内容,也是xml格式


CDATA内容不是XML文档。它是一种非结构化的文本数据,无论它从外部看起来如何。


这是CDATA的目的  - 存储一些东西,什么可以是什么 - 例如xml-with-formatting-error。


重要的是xml文档解析器不会解析CDATA的内容。


通过 

$获取CDATA的全部内容b $ b

 string toText = xmlDoc.OuterXml.Substring(xmlDoc.OuterXml.IndexOf("<![CDATA ["] +"<![CDATA [" .Length] ); 


或类似的结构。




<?xml version='1.0' encoding='UTF-8' ?> <root> <input> <![CDATA[ <ServerLicence>abc</ServerLicence> <inputData> <CreationPreferences></CreationPreferences> <SelectObj1> (?xml version="1.0" encoding="UTF-8"?)(FILTER CASESENSITIVE="TRUE" DEPTH="-1" RESULTSPERPAGE="-1" COMMAND="QUERY" ) (SELECT) (/SELECT) (WHERE) (CONDITION) (EXPRESSION) (VALUE) Replace_Placeholder (VALUE) (/EXPRESSION) (/CONDITION) (/WHERE) </SelectObj1> </inputData> ]]> </input> </root>

I have an xml file as seen, and it has some CData content, which is also xml format. I have a requirement to replace text inside Cdata- "Replace_Placeholder" in the code according to some arguments it recieves. So when there is more than 1 argument , the xml should look like:

<?xml version='1.0' encoding='UTF-8' ?>
<root>
  <input>
		<![CDATA[ 
			<ServerLicence>abc</ServerLicence> 
			<inputData>
				 <CreationPreferences></CreationPreferences>
				 
				 <SelectObj1>
				 (?xml version="1.0" encoding="UTF-8"?)(FILTER 
				 CASESENSITIVE="TRUE" DEPTH="-1" RESULTSPERPAGE="-1" 
				 COMMAND="QUERY" )
				 (SELECT)
				 (/SELECT)
				 (WHERE)
				   (CONDITION)
					(EXPRESSION)
					 (VALUE)
					  Replace_Placeholder
					 (VALUE)
					(/EXPRESSION)
				   (/CONDITION)
				 (/WHERE)
				</SelectObj1> 
				
				<SelectObj1>
				 (?xml version="1.0" encoding="UTF-8"?)(FILTER 
				 CASESENSITIVE="TRUE" DEPTH="-1" RESULTSPERPAGE="-1" 
				 COMMAND="QUERY" )
				 (SELECT)
				 (/SELECT)
				 (WHERE)
				   (CONDITION)
					(EXPRESSION)
					 (VALUE)
					  Replace_Placeholder
					 (VALUE)
					(/EXPRESSION)
				   (/CONDITION)
				 (/WHERE)
				</SelectObj1> 
			</inputData>
	]]>
  </input>
</root>

It should create one more node of type "SelectObj1" and replace text in Replace_Placeholder with the value from the argument. I am stuck as to how to create the same node and replace and finally save the xml! I hope my problem is clear!

解决方案

Hello, 

>CData content, which is also xml format

CDATA content is not an XML document. It is an unstructured text data, regardless to how it looks from outside.

This is a CDATA purpose - store something, what can be anything - xml-with-formatting-error - for example.

Important is that xml-document parser would NOT parse a content of CDATA.

Get whole content of CDATA you can by 

string toText = xmlDoc.OuterXml.Substring(xmlDoc.OuterXml.IndexOf("<![CDATA[") + "<![CDATA[".Length);

or similar construction.


这篇关于C#在CData中创建xml节点的副本(它本身在xml文件中)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆