获取UTF-8使用的XDocument大写的 [英] Get UTF-8 in Uppercase using XDocument

查看:454
本文介绍了获取UTF-8使用的XDocument大写的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有XML编码和版本,在我的XML文档的顶部,我正与的XDocument 制作。

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.

什么是我需要做什么?

我用的XDocument 类称为DOC一个新的XML文档。

I declare a new XML document using the XDocument class called 'doc'.

我保存这个使用 doc.Save()的文件;

我曾尝试:

  • doc.Declaration.Encoding.ToUpper();
  • 在声明一个新的 XDeclaration
  • 键入编码大写字母,我 doc.Declaration 设置为我的 XDeclaration
  • doc.Declaration.Encoding.ToUpper();
  • Declaring a new XDeclaration
  • Typing the Encoding in uppercase and setting my doc.Declaration to my XDeclaration.

这仍来自通过小写。

推荐答案

您可以创建自定义的XmlTextWriter ,例如:

You can create custom XmlTextWriter, e.g.:

public class CustomXmlTextWriter : XmlTextWriter
{
    public CustomXmlTextWriter(string filename)
        : base(filename, Encoding.UTF8)
    {

    }

    public override void WriteStartDocument()
    {
        WriteRaw("<?xml VERSION=\"1.0\" ENCODING=\"UTF-8\"?>");
    }

    public override void WriteEndDocument()
    {
    }
}

然后使用它:

Then use it:

using (var writer = new CustomXmlTextWriter("file.xml"))
{
    doc.Save(writer);
}

这篇关于获取UTF-8使用的XDocument大写的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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