Jump to content
  • Advertisement
Sign in to follow this  
Samurai Jack

[.net] XML encoding woes

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
A quick and dirty solution may be to replace the encoding attribute with the wished encoding. But I am not sure this will work fine. But a simple test can verify that.

Kimmi

Share this post


Link to post
Share on other sites
But if the XML file acctualy is ISO 1250 encoded, it makes no sense to overwrite the XML header and tell it its UTF8. The loader will load it wrong.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!