Jump to content

Localization from XML


DooVoo

Recommended Posts

Hi All,I'm currently trying to write a multi-lingual application that uses xml files to store the different content and an XmlTextReader to read in this content. This works fine for the english version but as soon as it tries to read the French version it throws a System.Xml.XmlException about the first french language specific character (i.e. an e with an accent). The exact exception error is:System.Xml.XmlException: There is an invalid character in the given encoding. Line 6, position 29.I believe it is down to the set encoding but I can't figure out a solution, everything looks to be using utf-8, which, from my reading, should work for both these languages. Even the System.Text.Encoding that is output if there is a problem says it's all in utf-8!If anyone can help here, I would be a very happy man!Here's an example of the code and xml file:myfile.aspx

<%@ Page Language="C#" Src="myfile.cs" Inherits="MyFile" ResponseEncoding="utf-8" Debug="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>	<title>MyFile</title>	<meta http-equiv="content-type"  content="text/html;charset=utf-8" />	<meta http-equiv="Content-Style-Type" content="text/css" /></head><body>        <asp:Literal id="PageTitle" runat="server" /></body></html>

myfile.cs

using System;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Text;// Used by Localizationusing System.Xml;public class MyFile : System.Web.UI.Page{    private Localization PageContent;    pubilc MyFile()    {        PageContent = new Localization("French");        //PageContent = new Localization("English");    }    public void Page_Load()    {        PageContent.GetData('PageTitle');    }}public class Localization{    private string XmlFile;    private XmlTextReader LocaleReader;    public Localization (string FileName)    {        XmlFile = FileName;    }        private LoadXml()    {        LocaleReader = XmlTextReader(XmlFile);    }    public string GetData(string XmlField)    {        string ElementValue = "Missing Xml Element";        LoadXml();        try        {            while (LocaleReader.Read())            {                LocaleReader.MoveToElement();                if (LocaleReader.Name == XmlField)                {                    ElementValue = LocaleReader.ReadString();                }            }        }        catch (System.Xml.XmlException)        {            ElementValue = "Xml Encoding Error [" + Encoding.GetEncoding(65001) + "]";        }        finally        {            LocaleReader.Close();        }        LocaleReader.Close();        return ElementValue;    }}

english.xml

<?xml version="1.0" encoding="utf-8"?><PageTitle>English</PageTitle>

french.xml

<?xml version="1.0" encoding="utf-8"?><PageTitle>Français</PageTitle>

Link to comment
Share on other sites

So after a weekend of scratching my head, researching different Localization technique, such as resource files and the resource manager I found the answer...and it made my cry.instead of:

<?xml version="1.0" encoding="utf-8"?>

use:

<?xml version="1.0" encoding="iso-8859-1"?>

From all my research utf-8 should support european characters but it causes the errors I saw. Changing the xml files encoding to iso-8859-1 works perfectly! Why couldn't I have found this out on Friday! :)

Link to comment
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
×
×
  • Create New...