使用 XDocument 获取大写的 UTF-8
我需要在使用 XDocument
制作的 XML 文档的顶部添加 XML 编码和版本。
我有这个,但它是小写的,并且需要是大写的。
我需要做什么?
我使用名为“doc”的 XDocument
类声明一个新的 XML 文档。
我使用 doc.Save(); 将其保存到文件中。
我尝试过:
doc.Declaration.Encoding.ToUpper();
- 声明一个新的
XDeclaration
- 以大写形式键入编码并设置我的
doc.Declaration
到我的XDeclaration
。
它仍然以小写形式出现。
I need to have XML encoding and version at the top of my XML document which I am making with XDocument
.
I have this but it is in lowercase, and it needs to be in uppercase.
What do I need to do?
I declare a new XML document using the XDocument
class called 'doc'.
I save this to a file using doc.Save();
.
I have tried:
doc.Declaration.Encoding.ToUpper();
- Declaring a new
XDeclaration
- Typing the Encoding in uppercase and setting my
doc.Declaration
to myXDeclaration
.
It still comes through in lowercase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建自定义
XmlTextWriter
,例如:然后使用它:
You can create custom
XmlTextWriter
, e.g.:Then use it:
使用 XmlDocument 的工作解决方案:
正如 user726720 指出的那样,Kirill Polishchuk 给出的答案丢失了格式并需要模式代码。
Working solution, using XmlDocument:
As user726720 pointed out, the answer give by Kirill Polishchuk losses the formatting and requires mode code.