ok. im try to make a multilanguage system based on that
tutorial :
http://www.123aspx.com/redir.aspx?res=29112
in my aspx pages all text are in labels , and i want to
take the name labels ( in one loop ) and for each label
take on the XML document the right(in the right language)
Value (text).
Above you can see what im doing , it works , however im
trying to make a function that look at all aspx and when
found a LABEL , take the value ( text) from one xml
document like the function in the end of this document.
**********************************************************
I have created an object that will retrieve the correct
data from an XML file. The code in C# is displayed below.
------------------------------------------------------
using System;
using System.Xml;
namespace diffudessnet
{
public class XmlLang
{
protected XmlDataDocument X;
protected String Lang;
public XmlLang(String FileName,String
Language)
{
String Name
= "
http://localhost/myapp/xml/"+ FileName;
X = new XmlDataDocument();
X.Load(Name);
Lang = Language;
}
public XmlLang(String FileName)
{
String Name
= "
http://localhost/myapp/xml/"+ FileName;
X = new XmlDataDocument();
X.Load(Name);
Lang = "N";
}
public String GetString(String Name)
{
XmlNode node;
node = X.SelectSingleNode
("//main/language[@id='" +Lang + "']/" + Name);
if (node == null) return "";
else
{
string value =
node.InnerXml.ToString();
return value;
}
}
}
}
------------------------------------------------------
This object has three methods:
XmlLang::XmlLang(String FileName, String Language)
XmlLang::XmlLang(String FileName)
String XmlLang::GetString(String Name)
To start, we will assume all XML documents are in the same
directory. In this way, they can be made inaccessible to
all users. The path of this directory is hardcoded in the
source of the object. A good alternative is using the
ConfigurationSettings.AppSettings method and saving all
values in the web.config file.
In the first version of the constructor, the name of the
XML file and the selected language are passed to the
constructor. If you want to select a name for the
document, it might be useful to select the same name as
you aspx file. The language parameter is a simple string.
Possible values are "N" for Dutch, "F" for French, "E" for
English, . The format of the XML document will be
discussed later.
The second version of the constructor will automatically
select the Dutch version of the texts. This option is very
handy if you only want to use the Dutch version, and other
languages are only planned for the future.
The method GetString(String Name) will retrieve the text
belonging to the node named Name from the XML file. All
text items in the XML document are marked by keywords. The
value of a keyword is the word in the specified language.
-------------------------------------------
XML Document
<?xmlversion="1.0"encoding="utf-8"?>
<main>
< language id ="N">
<name>Naam</ name>
<number>Nummer</number>
<street>Straat</street>
<country>Land</country>
</ language >
< language id ="E">
<name>Name</name>
<number>Number</number>
<street>Street</street>
<country>Country</country>
</language>
<languageid="F">
<name>Nom</name>
<number>Numméro</number>
<street>Rue</street>
<country>Pays</country>
</language>
</main>
Different languages are separated by the <language
id="languageid" /> tags. The languageid attribute should
be passed to the constructor of the class.
Using the class
Using the object is pretty straightforward. You create a
new instance of the object in the OnInit(.) member
function of your page object. Suppose we have a page
called client.aspx containing client data. The name of the
codebehind file is client.aspx.cs. To make things simple,
we name the xml file client.xml.
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
XmlLang Lang = new XmlLang("client.xml",Session
["LANGUAGE"].ToString());
this.NameLabel.Text = Lang.GetString("name");
this.StreetLabel.Text = Lang.GetString("street");
this.CountryLabel.Text = Lang.GetString("country");
}
Instead of hard-coding all static text in client.aspx, we
use labels as a replacement. In the OnInit member
function, these labels are filled with the right contents,
depending on the selected language. In this example, the
chosen language is stored in a session variable : Session
["LANGUAGE"]. The method as it is mentioned also allows
you to change column headers from a datagrid object.
**********************************************************