Koti Raavi
Koti Raavi

Reputation: 310

Converting XML to UTF-8 using C#

I have written below code to convert XML file to UTF-8 format file, it is working as excepted but issue is header is concatenating with body text instead of writing in separate line. I need utf8 in seperate line but file.writealltext will not accept more than 3 arguments/parameters. Any help appreciated.

        string path = @"samplefile.xml";
        string path_new = @"samplefile_new.xml";

        Encoding utf8 = new UTF8Encoding(false);
        Encoding ansi = Encoding.GetEncoding(1252);

        string xml = File.ReadAllText(path, ansi);

        XDocument xmlDoc = XDocument.Parse(xml);

        File.WriteAllText(
            path_new,
            @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""true"">" + xmlDoc.ToString(),

           utf8
        );

Upvotes: 0

Views: 1419

Answers (1)

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22303

No need to use any API other than LINQ to XML. It has all means to deal with XML file encoding, prolog, BOM, indentation, etc.

void Main()
{
    string outputXMLfile = @"e:\temp\XMLfile_UTF-8.xml";

    XDocument xml = XDocument.Parse(@"<?xml version='1.0' encoding='utf-16'?>
                    <root>
                        <row>some text</row>
                    </root>");

    XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", null),
        new XElement(xml.Root)
    );


    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "\t";
    // to remove BOM
    settings.Encoding = new UTF8Encoding(false);

    using (XmlWriter writer = XmlWriter.Create(outputXMLfile, settings))
    {
        doc.Save(writer);
    }
}

Upvotes: 1

Related Questions