简体   繁体   English

使用c#提取和修改xml的cdata部分

[英]Extract and Modify cdata section of xml using c#

I have the following xml 我有以下xml

<parent>
    <child>abra</child>
    <child>kadabra</child>
    <child>alakazam</child>
    <![CDATA[ some data here ]]>
</parent>

I want to extract the cdata section, what I have done is - converted the data into string and extracted it with the following code 我想提取cdata部分,我所做的是 - 将数据转换为字符串并使用以下代码将其解压缩

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

where xmlDoc is XMLDocument which contails above xml 其中xmlDoc是XMLDocument,它包含xml以上

Is there any better way of doing this? 有没有更好的方法呢?

I googled a lot, but what I got is extraction of cdata section only if its the only child of its parent element. 我google了很多,但我得到的是cdata部分的提取,只要它是其父元素的唯一子元素。

Finally I want to modify cdata section and modify the current xml as 最后我想修改cdata部分并修改当前的xml为

<parent>
    <child>abra</child>
    <child>kadabra</child>
    <child>alakazam</child>
    <![CDATA[ modified data here ]]>
</parent>

Given this valid XML sample : 鉴于这个有效的 XML示例:

<parent>
    <child>1</child>
    <child>2</child>
    <child>3</child>
    <![CDATA[ some data here ]]>
</parent>

Since the only text node that is direct child of <parent> is the cdata section you want to get, you can do this way to select the cdata section and modify it's content : 由于<parent>直接子节点唯一的文本节点是您想要获取的cdata节,因此您可以通过这种方式选择cdata节并修改其内容:

var cdata = (XmlCDataSection)xmlDoc.SelectSingleNode("/parent/text()");
cdata.InnerText = " modified data here ";
Console.WriteLine(xmlDoc.OuterXml);

Another possible approach is using XDocument to replace the older library, XmlDocument : 另一种可能的方法是使用XDocument替换旧的库, XmlDocument

var doc = XDocument.Load("path_to_your_xml");
var xcdata = doc.DescendantNodes().OfType<XCData>().FirstOrDefault();
xcdata.Value = " modified data here ";
Console.WriteLine(doc.ToString());

The output is as follow (formatted for readability) : 输出如下(为便于阅读而格式化):

<parent>
    <child>1</child>
    <child>2</child>
    <child>3</child>
    <![CDATA[ modified data here ]]>
</parent>

转换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 - CDATA in XML using C# 使用CData节包装属性-XML序列化C# - Wrap properties with CData Section - XML Serialization C# 在C#中解码CDATA部分 - Decode CDATA section in C# C# 中的 XML CDATA 读取 - XML CDATA reading in C# 在C#中处理大型CDATA部分 - Processing large CDATA section in C# 使用 Linq 获取 CDATA XML 部分 - Getting CDATA XML Section Using Linq 如何在C#中将XML转换为JSON的过程中忽略HTML内容的#cdata-section - How to ignore #cdata-section of HTML content during XML to JSON conversion in C# 使用Linq-to-XML和C#阅读RSS feed-如何解码CDATA部分? - Reading RSS feed with Linq-to-XML and C# - how to decode CDATA section? 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#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM