我正在将一串 XML 解析成一个看起来像这样的 XDocument(使用 XDocument.Parse)
<Root>
<Item>Here is "Some text"</Item>
</Root>
然后我稍微操作一下 XML,我想把它作为一个字符串发送回来,就像它进来一样
<Root>
<Item>Here is "Some text"</Item>
<NewItem>Another item</NewItem>
</Root>
但是,我得到的是
<Root>
<Item>Here is \"Some text\"</Item>
<NewItem>Another item</NewItem>
</Root>
请注意双引号现在是如何转义而不是编码的?
无论我使用,都会发生这种情况
ToString(SaveOptions.DisableFormatting);
或者
var stringWriter = new System.IO.StringWriter();
xDoc.Save(stringWriter, SaveOptions.DisableFormatting);
var newXml = stringWriter.GetStringBuilder().ToString();
我怎样才能有双引号出来"
而不是\"
?
更新:也许这可以更好地解释它:
var origXml = "<Root><Item>Here is \"Some text"</Item></Root>";
Console.WriteLine(origXml);
var xmlDoc = System.Xml.Linq.XDocument.Parse(origXml);
var modifiedXml = xmlDoc.ToString(System.Xml.Linq.SaveOptions.DisableFormatting);
Console.WriteLine(modifiedXml);
我从中得到的输出是:
<Root><Item>Here is "Some text"</Item></Root>
<Root><Item>Here is "Some text"</Item></Root>
我希望输出为:
<Root><Item>Here is "Some text"</Item></Root>
<Root><Item>Here is "Some text"</Item></Root>