Sunday, February 11, 2007

Determine the Encoding of an XML File: Method 1

In Visual Studio 2003, I needed a way to determine the encoding of an XML file. I found some information about reading the byte order mark (BOM) on a file, but I couldn't get it to work properly. The alternative approach below seems to be working. In this example, I needed the file to be UTF-8.
String encoding = DetectEncodingXml(); 
if(encoding.IndexOf("UTF-8") > -1)
{
/* This is what we want. Run the code. */
}
else
{
/* Display error message. */
}
private string DetectEncodingXml()
{
try
{
XmlTextReader xreader = new XmlTextReader(openFileDialog.FileName);
xreader.Read();
Encoding xreaderEnc = xreader.Encoding;
string xreaderEncString = xreaderEnc.EncodingName;
return xreaderEncString;
}
catch(Exception ex)
{
return " Encoding could not be detected. ";
}
}

No comments: