C# 使用XMLCDATA节设置XmlDocument格式

C# 使用XMLCDATA节设置XmlDocument格式,c#,xmldocument,C#,Xmldocument,当我将XMLCDATA节添加到我的XmlDocument时,它会弄乱我的Xml格式 string newFileName = @"C:\temp\test.xml"; XmlDocument xd = new XmlDocument(); xd.LoadXml("<item><name>wrench</name></item>"); XmlCDataSection c = xd.CreateCDataSection("mycdatacontent"

当我将XMLCDATA节添加到我的XmlDocument时,它会弄乱我的Xml格式

string newFileName = @"C:\temp\test.xml";
XmlDocument xd = new XmlDocument();
xd.LoadXml("<item><name>wrench</name></item>");
XmlCDataSection c = xd.CreateCDataSection("mycdatacontent");
xd.ChildNodes[0].AppendChild(c);
xd.Save(newFileName);
string newFileName=@“C:\temp\test.xml”;
XmlDocument xd=新的XmlDocument();
加载XML(“扳手”);
XMLCDATA节c=xd.createCDATA节(“MyCDATA内容”);
xd.ChildNodes[0].AppendChild(c);
保存(newFileName);
输出如下所示:

<item>
  <name>wrench</name><![CDATA[mycdatacontent]]></item>
<item>
  <name>wrench</name>
  <![CDATA[mycdatacontent]]>
</item>

扳手
但它应该是这样的:

<item>
  <name>wrench</name><![CDATA[mycdatacontent]]></item>
<item>
  <name>wrench</name>
  <![CDATA[mycdatacontent]]>
</item>

扳手
我如何更改它,使输出看起来像我期望的那样?
CDATA部分需要在单独的行中。

尝试
XmlWriter
XmlWriterSettings.Indent
。文档中有一个示例。但你想要什么其实不是一个标准的方式。此CDATA节未封装在其自己的元素中。可能没有办法,或者您还必须手动插入一些
XmlWhitespace
XmlWriterSettings.Indent
没有更改文件的格式。现在唯一的区别是,它已经自动创建了XML头。