Get UTF-8 in Uppercase using XDocument

寵の児 提交于 2019-12-04 05:10:29
Kirill Polishchuk

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);
}

Working solution, using XmlDocument:

 myXmldoc.FirstChild.Value = "version=\"1.0\" encoding=\"UTF-8\""; 

As user726720 pointed out, the answer give by Kirill Polishchuk losses the formatting and requires mode code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!