Get UTF-8 in uppercase using XDocument

I need to have an encoding and version of XML at the top of my XML document that I create using XDocument .

I have this, but it is lowercase and it should be uppercase.

What do I need to do?

I am declaring a new XML document using an XDocument class called "doc".

I save it in a file using doc.Save(); .

I tried:

  • doc.Declaration.Encoding.ToUpper();
  • New XDeclaration
  • Enter the upper case encoding and set my doc.Declaration to my XDeclaration .

It is still lowercase.

+6
source share
1 answer

You can create a custom XmlTextWriter , for example:

 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); } 
+4
source

Source: https://habr.com/ru/post/903991/


All Articles