JMK
JMK

Reputation: 28059

Get UTF-8 in Uppercase using 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.

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:

It still comes through in lowercase.

Upvotes: 7

Views: 2567

Answers (2)

ana boies
ana boies

Reputation: 155

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.

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

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

Upvotes: 4

Related Questions