我正在创建一个类似于这样的XDocument:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"));
当我用这种方式保存文档(doc.Save(@"c:\tijd\file2.xml");
)时,我得到了这个:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
这是可以的。
但我想要将内容作为 XML 返回,我找到了下面的代码:
var wr = new StringWriter();
doc.Save(wr);
string s = (wr.GetStringBuilder().ToString());
这段代码可以运行,但之后字符串's'以此开头:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
它从utf8变成了utf16,而这不是我想要的,因为现在我无法在Internet Explorer中阅读它。
有没有一种方法可以防止这种行为发生?