如何打印使用 XDocument
使用 ToString 方法时,有什么方法可以让 XDocument 打印 xml 版本吗?让它输出类似这样的内容:
<?xml version="1.0"?>
<!DOCTYPE ELMResponse [
]>
<Response>
<Error> ...
我有以下内容:
var xdoc = new XDocument(new XDocumentType("Response", null, null, "\n"), ...
它将打印这个,这很好,但它缺少如上所述的“
<!DOCTYPE ELMResponse [
]>
<Response>
<Error> ...
我知道你可以通过我自己手动输出来做到这一点。只是想知道是否可以使用 XDocument。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过使用 XDeclaration。这将添加声明。
但使用
ToString()
您将无法获得所需的输出。您需要将 XDocument.Save() 与他的方法之一一起使用。
完整样本:
By using XDeclaration. This will add the declaration.
But with
ToString()
you will not get the desired output.You need to use
XDocument.Save()
with one of his methods.Full sample:
这是迄今为止最好的方法,也是最容易管理的:
我这么说只是因为您可以通过将 .UTF8 更改为 .Unicode 或 .UTF32 来将编码更改为任何内容
This is by far the best way and most managable:
and i say that just because you can change encoding to anything by changing .UTF8 to .Unicode or .UTF32
对一个老问题的迟到回答,但我将尽力提供比其他答案更多的细节。
您所询问的内容称为 XML 声明。
首先,
XDocument
有一个属性XDeclaration
为此。您可以使用XDocument
构造函数的另一个重载:或者稍后设置该属性:
但这取决于您
保存或写入的方式XDocument
之后,声明(或其部分)可能会被忽略。稍后会详细介绍。XML 声明可以有多种外观。以下是一些有效的示例:
请注意,
XDeclaration
很乐意接受无效参数,因此您需要确保其正确。在许多情况下,第一个
(您要求的形式)是完美的(不需要提供
encoding
如果它只是 UTF-8(包括 ASCII),并且如果其预期值为"no"
或没有 DTD,则不需要指定standalone
)。请注意,
xdoc.ToString()
会从XNode
基类(在我的 .NET 版本中)进行覆盖,并且不包含 XML 声明。您可以轻松地创建一个方法来处理这个问题,如下所示:其他一些答案表明,如果您使用
xdoc.Save
或,则将遵守
方法,但这并不完全正确:XDeclaration
>xdoc.WriteToXDocument
中没有 XML 声明,它们也可能包含 XML 声明。XDeclaration
中这样做,则不是省略编码,1.1
更改为1.0
当然,当您保存/写入文件时,声明与该文件的真实编码相匹配是一件好事!
但有时,当您写入内存中的字符串时,您不需要
utf-16
(即使您意识到 .NET 字符串内部采用 UTF-16)。您可以使用上面的扩展方法来代替。或者您可以使用 EricSch 的答案中的以下方法的破解版本:您有:
加法。无论是谁更新了类型
StringWriter
(及其基类型TextWriter
)以使用nuallable 引用类型,都决定使用诸如编码
和FormatProvider
不应该可以为空。也许这很不幸?无论如何,如果您打开了可为 null 的引用类型,则可能需要编写null!
而不仅仅是null
。Late answer to an old question, but I shall try to provide more details than the other answers.
The thing you ask about, is called an XML declaration.
First of all, the
XDocument
has a propertyDeclaration
of typeXDeclaration
for this. You can either user another overload of theXDocument
constructor:or set the property later:
But depending on how you save or write your
XDocument
later, the declaration (or parts of it) may be ignored. More on that later.The XML declaration can have a number of appearances. Here are some valid examples:
Note that
XDeclaration
will happily accept invalid arguments, so it is up to you to get it right.In many cases the first one,
<?xml version="1.0"?>
, the form you ask for, is perfect (it is not needed to giveencoding
if it is just UTF-8 (including ASCII), and it is not needed to specifystandalone
if its intended value is"no"
or if there are no DTDs).Note that
xdoc.ToString()
goes do the override from theXNode
base class (in my version of .NET) and does not include the XML declaration. You can easily enough create a method to deal with that, like this:Some of the other answers indicate that the
XDeclaration
will be respected if you usexdoc.Save
orxdoc.WriteTo
methods, but that is not quite true:XDocument
XDeclaration
1.1
into1.0
Of course, when you save/write to a file, it is a good thing that the declaration matches the true encoding of that file!
But sometimes when you write to a string in mememory, you do not want the
utf-16
(even if you realize that .NET strings are in UTF-16 internally). You can use the extension method above instead. Or you can use the following hacked version of the method from EricSch's answer:where you have:
Addition. Whoever updated the type
StringWriter
(and its base typeTextWriter
) to use nuallable reference types, decided that properties such asEncoding
andFormatProvider
should not be nullable. Maybe that was unfortunate? Anyway, if you have nullable reference types turned on, you may need to writenull!
instead of justnull
.只要输入这个
你就会得到
Just type this
And you get
VB.NET解决方案代码
代码
输出 注意(请在记事本中打开输出)
VB.NET Solution CODE
Code
Output Note(please open output in notepad )
更简单的方法是:
如果您的 xDocument.Declaration 为空,只需添加它即可。
The easier way is:
If your xDocument.Declaration is empty, just add it.