thndrkiss
thndrkiss

Reputation: 4595

iso-8859-1 to utf-8 in c#

Is there a way to transform a xml file which is iso-8859-1 to utf-8 ?

Upvotes: 2

Views: 1880

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

The simplest way would be to load it and then save it with one of the XML APIs available. That way any extra transformations (e.g. the XML declaration) will be handled appropriately. For example:

using System;
using System.Text;
using System.Xml.Linq;

class Test
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load("test.xml");
        XDeclaration declaration = doc.Declaration;
        if (declaration != null) {
            declaration.Encoding = "utf-8";
        }
        doc.Save("test-utf8.xml");
    }
}

Note that I think this may end up changing some things around indentation etc, unless you specify some extra options. Is that likely to be a problem for you?

You could potentially just load the whole file as text (using Encoding.GetEncoding(28591)), modify the declaration part yourself and resave it in UTF-8 yourself. I suspect there may be some corner cases where that would cause a problem though.

Upvotes: 3

Related Questions