ReadAppConfig.cs
using System;
using System.Collections;
using System.XML;
using System.IO;
using System.Windows.Forms;
using System.Text;
Namespace MuchLanguagenamespace MuchLanguage
{
/// <summary>
/// ReadAppResource 的摘要說明。
/// </summary>
public Class ReadAppResourceclass ReadAppResource
{
public ReadAppResource()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public Hashtable ReadResource(string frmName,string lang)
{
Hashtable result=new Hashtable();
XMLReader reader=null;
FileInfo fi=new FileInfo("AppResource_"+lang+".XML");
if(!fi.Exists)
{
// &n reader=new XmlTextReader(System.Environment.CurrentDirectory.ToString()+"AppResource.XML");
reader=new XmlTextReader( Application.ExecutablePath +"AppResource.XML");
}
else
{
reader=new XmlTextReader("AppResource_"+lang+".XML");
}
XmlDocument doc=new XMLDocument();
doc.Load(reader);
XMLNode root=doc.DocumentElement;
XMLNodeList nodelist=root.SelectNodes("Form[Name=''" + frmName + "'']/Controls/Control");
foreach(XMLNode node in nodelist)
{
try
{
XMLNode node1=node.SelectSingleNode("@name");
XMLNode node2=node.SelectSingleNode("@text");
if(node1!=null)
{result.Add(node1.InnerText.ToLower(),node2.InnerText);}
}
catch(FormatException fe)
{Console.WriteLine(fe.ToString());}
}
reader.Close();
return result;
}
//在N中處理XML數據是很方便的,但是對於初學者來說,可能有時候還是有些困難,
//以下代碼是將表轉換成XMLDocument,然後在讀取出來的示例,很簡單,可以自己在進行擴充.
private XmlDocument CreateXMLDocument()
{
XmlDocument xmldoc = new XMLDocument();
XMLNode xnod;
XMLElement xenl;
//描述信息
xnod = xmldoc.CreateNode(XmlNodeType.XMLDeclaration, "", "");
xnod.InnerText += "encoding=''UTF-8''";
XMLdoc.AppendChild(xnod);
//根元素
xenl = XMLdoc.CreateElement("", "body", "");
XMLdoc.AppendChild(xenl);
XmlNode nod = XMLdoc.SelectSingleNode("body");
//設置元素的值
XMLElement element;
element = XMLdoc.CreateElement("item");
element.SetAttribute("Name","Manqing Yu");
element.SetAttribute("Age","24");
element.SetAttribute("Sex", "男");
nod.AppendChild(element);
xmldoc.Save("c:XmlTest.XML");
return XMLdoc;
}
}
}
ReadAppResource.cs
using System;
using System.Collections;
using System.XML;
using System.Text;
Namespace MuchLanguagenamespace MuchLanguage
{
/// <summary>
/// ReadppConfig 的摘要說明。
/// </summary>
public Class ReadAppConfigclass ReadAppConfig
{
public ReadAppConfig()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public IList GetLanguageList(string lang)
{
IList result =new ArrayList ();
XmlReader reader=new XmlTextReader("AppConfig.XML");
XmlDocument doc=new XMLDocument();
doc.Load(reader);
XMLNode root=doc.DocumentElement;
XMLNodeList nodelist=root.SelectNodes("Area[Language=''"+lang +"'']/List/Item");
foreach(XMLNode node in nodelist)
{
try
{
if(node!=null)
{result.Add(node.InnerText);}
}
catch(FormatException fe)
{Console.WriteLine(fe.ToString());}
}
reader.Close();
return result;
}
public string GetChildLanguage(string Setlang,string lang)
{
IList result =new ArrayList ();
XmlReader reader=new XmlTextReader("AppConfig.XML");
XmlDocument doc=new XMLDocument();
doc.Load(reader);
XMLNode root=doc.DocumentElement;
XMLNodeList nodelist=root.SelectNodes("Area[Language=''"+lang +"'']/List/Item"); &nbs foreach(XMLNode node in nodelist)
{
try
{
if(node!=null)
{
if (Setlang==node.InnerText)
{
int len=Setlang.Length;
Setlang=Setlang.Substring(len-4);
Setlang=Setlang.Substring(1,2);
}
}
}
catch(FormatException fe)
{Console.WriteLine(fe.ToString());}
}
reader.Close();
return Setlang;
}
public string Getfullnamelanguage(string lang)
{
string Getlanguage=null;
IList result =new ArrayList ();
Xreader=new XmlTextReader("AppConfig.XML");
XmlDocument doc=new XMLDocument();
doc.Load(reader);
XMLNode root=doc.DocumentElement;
XMLNodeList nodelist=root.SelectNodes("Area[Language=''"+lang +"'']/List/Item");
foreach(XMLNode node in nodelist)
{
try
{
if(node!=null)
{
string Setlang=node.InnerText;
int len=Setlang.Length;
Setlang=Setlang.Substring(len-4);
Setlang=Setlang.Substring(1,2);
if (Setlang==lang)
{
Getlanguage=node.InnerText;
}
}
}
catch(FormatException fe)
{Console.WriteLine(fe.ToString());}
}
reader.Close();
return Getlanguage;
}
}
}
ReadDefaultLanguage.cs
using System;
using System.XML;
using System.Data;
using System.Collections;
using System.Windows.Forms;
Namespace MuchLanguagenamespace MuchLanguage
{
/// <summary>
/// ReadDefaultLanguage 的摘要說明。
/// </summary>
public Class ReadDefaultLanguageclass ReadDefaultLanguage
{
public ReadDefaultLanguage()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public string ReadDefaultLanguages()
{
& }
XmlReader reader=new XmlTextReader("LanguageDefine.XML");
XmlDocument doc =new XMLDocument();
doc.Load(reader);
XMLNode root=doc.DocumentElement;
XMLNode node=root.SelectSingleNode("DefaultLanguage");
string result="EN";
if (node!=null)
result=node.InnerText;
reader.Close();
return result;
}
public void WriteDefaultLanguage(string lang)
{
DataSet ds=new DataSet();
ds.ReadXml("LanguageDefine.XML");
DataTable dt=ds.Tables["Language"];
dt.Rows[0]["DefaultLanguage"]=lang;
ds.AcceptChanges();
ds.WriteXml("LanguageDefine.XML");
}
public void getNames(Form form,string lang)
{
ReadAppResource AppConfig=new ReadAppResource();
Hashtable table=AppConfig.ReadResource(form.Name,lang);
Control.ControlCollection controlNames=form.Controls;
try
{
foreach(Control control in controlNames)
XmlReader reader=new XmlTextReader("LanguageDefine.XML");
XmlDocument doc =new XMLDocument();
doc.Load(reader);
XMLNode root=doc.DocumentElement;
XMLNode node=root.SelectSingleNode("DefaultLanguage");
string result="EN";
if (node!=null)
result=node.InnerText;
reader.Close();
return result;
}
public void WriteDefaultLanguage(string lang)
{
DataSet ds=new DataSet();
ds.ReadXml("LanguageDefine.XML");
DataTable dt=ds.Tables["Language"];
dt.Rows[0]["DefaultLanguage"]=lang;
ds.AcceptChanges();
ds.WriteXml("LanguageDefine.XML");
}
public void getNames(Form form,string lang)
{
ReadAppResource AppConfig=new ReadAppResource();
Hashtable table=AppConfig.ReadResource(form.Name,lang);
Control.ControlCollection controlNames=form.Controls;
try
{
foreach(Control control in controlNames)
{
if(control.GetType()==typeof(System.Windows.Forms.Panel))
GetSubControls(control.Controls,table);
if(control.GetType()==typeof(System.Windows.Forms.GroupBox))
GetSubControls(control.Controls,table);
if(table.Contains(control.Name.ToLower()))
control.Text=(string)table[control.Name.ToLower()];
}
if(table.Contains(form.Name.ToLower()))
form.Text=(string)table[form.Name.ToLower()];
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void GetSubControls(Control.ControlCollection controls,Hashtable table)
{
foreach(Control control in controls)
{
if(control.GetType()==typeof(System.Windows.Forms.Panel))
GetSubControls(control.Controls,table);
if(control.GetType()==typeof(System.Windows.Forms.GroupBox))
GetSubControls(control.Controls,table);
if(table.Contains(control.Name.ToLower()))
control.Text=(string)table[control.Name.ToLower()];
}
}
}
}