尝试将xml文本转换为xml

时间:2016-12-26 21:16:32

标签: c# xml xml-parsing

我试图将源系统中的XML转换为c#

中的相同xml

# Merge all streams into stdout
$result = dotnet test *>&1

# Evaluate success/failure
if($LASTEXITCODE -eq 0)
{
    # Success
}
else
{
    # Failed, you can reconstruct stderr strings with:
    $ErrorString = $result -join [System.Environment]::NewLine
}

<root>
 <child>&lt;xml&gt;&lt;/xml&gt;</child>
</root>

我试着跟随到达

<root>
 <child><![CDATA[<xml></xml>]]></child>
</root>

以下输出

var node = resXML.SelectSingleNode(@"/root/child");
node.ParentNode.ReplaceChild(node.AppendChild(resXML.CreateCDataSection(encodedXML)), node);

2 个答案:

答案 0 :(得分:0)

试试这个

var sourceXml = "<root><child>&lt;xml&gt;&lt;/xml&gt;</child></root>";

var source = XDocument.Parse(sourceXml);

var result = new XDocument(new XElement("root",
                    source.Root
                          .Elements("child")
                          .Select(e => new XElement("child", new XCData(e.Value)))));

var resultXml = result.ToString();

答案 1 :(得分:0)

试试这个:

string xml = "<root><child>&lt;xml&gt;&lt;/xml&gt;</child></root>";

XmlDocument resXML = new XmlDocument();
resXML.LoadXml(xml);
var node = resXML.SelectSingleNode(@"/root/child");
node.AppendChild(resXML.CreateCDataSection(node.InnerText));
node.RemoveChild(node.FirstChild);

string output = resXML.OuterXml;