问题描述
有没有办法让使用ToString方法时,一个XDocument打印的xml版本?有它的输出是这样的:
Is there any way to have an XDocument print the xml version when using the ToString method? Have it output something like this:
<?xml version="1.0"?>
<!DOCTYPE ELMResponse [
]>
<Response>
<Error> ...
我有以下内容:
var xdoc = new XDocument(new XDocumentType("Response", null, null, "\n"), ...
这将打印此这是很好的,但它缺少的。如上述&LT?XML版本
which will print this which is fine, but it is missing the "<?xml version" as stated above.
<!DOCTYPE ELMResponse [
]>
<Response>
<Error> ...
我知道,你可以通过手动将其输出我自己做到这一点。只是想知道是否有可能使用的XDocument。
I know that you can do this by outputting it manually my self. Just wanted to know if it was possible by using XDocument.
推荐答案
通过使用XDeclaration。这将增加的声明。
By using XDeclaration. This will add the declaration.
但随着你的ToString不会得到所需的输出。
But with ToString you will not get the desired output.
您需要用他的方法之一,利用XDocument.Save()。
You need to use XDocument.Save() with one of his methods.
全样本:
var doc = new XDocument(
new XDeclaration("1.0", "utf-16", "yes"),
new XElement("blah", "blih"));
var wr = new StringWriter();
doc.Save(wr);
Console.Write(wr.GetStringBuilder().ToString());
这篇关于如何打印&LT; XML版本=&QUOT; 1.0 QUOT;&GT;使用的XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!