如何打印< XML版本=" 1.0 QUOT;>使用的XDocument [英] How to print <?xml version="1.0"?> using XDocument

查看:172
本文介绍了如何打印< XML版本=" 1.0 QUOT;>使用的XDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆