Sign in to follow this  
Samurai Jack

[.net] XML encoding woes

Recommended Posts

Hello! I have a problem. We have several customers sending us XML files, but they are almost never in UTF8. Some are with encoding Windows 1250 or some are with encoding ISO-8859-2. But the xml files are at least declared with the encoding flag. So far so good. What I would like to do is to convert this XML files into UTF8. The code I was using was like this:
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes ( xml.OuterXml );
The problem? The data array might be UTF8 right now, but in the XML header it still says it's ISO-8859-2. May I send souch data UTF8 buffer forwar regardless its xml declaration says it's ISO-8859-2? Does anyone have a better suggestion? I mean something has to be allready done in C#. I don't want to reinvent the wheel. Thank you in advance!

Share this post


Link to post
Share on other sites
Here's a way to convert a xml file from it's original encoding to UTF-8

public static void ConvertToUTF8Encoding(string fileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
XmlWriter xw = XmlWriter.Create(fileName + ".output", settings);
doc.Save(xw);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this