C# XML读写
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Collections;
using System.Reflection;
namespace Rca4.libLng
{
public class ClassLng
{
private bool IsCreateXmlItems = true;
private static string _lngCode = "";
private static Hashtable lngUI = null;
private static Hashtable lngMsg = null;
private string _LanguagePath;
private string _Prefix;
private string _Default;
XmlDocument xmlDoc;
XmlDocument xmlMsgDoc;
/// <summary>
/// 语言对象初始化
/// </summary>
/// <param name="LanguagePath">语言包目录</param>
/// <param name="Prefix">语言包文件前缀</param>
/// <param name="Default">默认语言标识</param>
public ClassLng(string LanguagePath, string Prefix,string Default)
{
_LanguagePath = LanguagePath;
_Prefix = Prefix;
_Default = Default;
if (IsCreateXmlItems)
{
string xmlData = @"<?xml version='1.0' encoding='utf-8' ?>
<Resource>
<Messages>
</Messages>
<Items>
</Items>
</Resource>";
xmlDoc = new XmlDocument();
xmlDoc.Load(new StringReader(xmlData));
xmlMsgDoc = new XmlDocument();
FileInfo fi = new FileInfo("Msg.xml");
if (!fi.Exists)
{
xmlMsgDoc.Load(new StringReader(xmlData));
}
else
{
XmlReader reader = new XmlTextReader("Msg.xml");
xmlMsgDoc.Load(reader);
}
}
}
public string lngCode
{
get
{
return _lngCode;
}
set
{
if (_lngCode != value)
{
_lngCode = value;
this.ReadResource();
}
}
}
/// <summary>
/// 读取语言消息
/// </summary>
/// <param name="Name">语言消息名称</param>
/// <param name="Default">默认值</param>
public string Read(string Name)
{
if (IsCreateXmlItems)
{
//加入一个元素
bool IsNotFind = true;
XmlNodeList nodeList = xmlMsgDoc.SelectSingleNode("Resource/Messages").ChildNodes;
foreach(XmlNode node in nodeList)
{
if (node.Attributes["key"].InnerText == Name.ToLower())
{
IsNotFind = false;
break;
}
}
if (IsNotFind)
{
XmlNode NodeItems = xmlMsgDoc.SelectSingleNode("Resource/Messages");
XmlElement xeitem = xmlMsgDoc.CreateElement("Message");//创建一个<Item>节点
xeitem.SetAttribute("key", Name.ToLower());//设置该节点key属性
xeitem.InnerText = Name;
NodeItems.AppendChild(xeitem);
xmlMsgDoc.Save("Msg.xml");
}
}
if (lngMsg.Contains(Name.ToLower()))
{
return (string)lngMsg[Name.ToLower()];
}
else
{
return Name;
}
}
/// <summary>
/// 读取语言包
/// </summary>
private void ReadResource()
{
lngUI = new Hashtable();
lngMsg = new Hashtable();
XmlReader reader = null;
FileInfo fi = new FileInfo(_LanguagePath + "\\" + _Prefix + _lngCode + ".xml");
if (!fi.Exists)
reader = new XmlTextReader(_LanguagePath + "\\" + _Prefix + _Default + ".xml");
else
reader = new XmlTextReader(_LanguagePath + "\\" + _Prefix + _lngCode + ".xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode root = doc.DocumentElement;
XmlNodeList nodelist1 = root.SelectNodes("Items/Item");
foreach (XmlNode node in nodelist1)
{
try
{
XmlNode node1 = node.SelectSingleNode("@key");
if (node1 != null)
{
lngUI.Add(node1.InnerText.ToLower(), node.InnerText);
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
}
}
//消息语言
XmlNodeList nodelist2 = root.SelectNodes("Messages/Message");
foreach (XmlNode node in nodelist2)
{
try
{
XmlNode node1 = node.SelectSingleNode("@key");
if (node1 != null)
{
lngMsg.Add(node1.InnerText.ToLower(), node.InnerText);
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
}
}
reader.Close();
}
/// <summary>
/// 更改语言
/// </summary>
/// <param name="form">要更改语言的窗口</param>
public void ChangeLngNames(Form form)
{
string[] Propertys = { "Text", "Caption"};
int Pro_reg;
string lngText="";
if (IsCreateXmlItems)
{
//加入一个元素
XmlNode NodeItemsFrm = xmlDoc.SelectSingleNode("Resource/Items");
XmlElement xeitemFrm = xmlDoc.CreateElement("Item");//创建一个<Item>节点
xeitemFrm.SetAttribute("key", form.Text.ToLower());//设置该节点key属性
xeitemFrm.InnerText = form.Text;
NodeItemsFrm.AppendChild(xeitemFrm);
}
if (lngUI.Contains(form.Text.ToLower()))
{
form.Text = (string)lngUI[form.Text.ToLower()];
}
System.Reflection.FieldInfo[] fieldInfo = form.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
for (int i = 0; i < fieldInfo.Length; i++)
{
for (Pro_reg=0; Pro_reg < Propertys.Length; Pro_reg++)
{
lngText = GetPropertyValue(fieldInfo[i].GetValue(form), Propertys[Pro_reg]).ToString();
if (lngText != "")
{
break;
}
}
if (lngText != "")
{
if (lngUI.Contains(lngText.ToLower()))
{
if (IsCreateXmlItems)
{
XmlNode NodeItems = xmlDoc.SelectSingleNode("Resource/Items");
XmlElement xeitem = xmlDoc.CreateElement("Item");//创建一个<Item>节点
xeitem.SetAttribute("key", lngText.ToLower());//设置该节点key属性
xeitem.InnerText = lngText;
NodeItems.AppendChild(xeitem);
}
//设置语言值
SetPropertyValue(fieldInfo[i].GetValue(form), Propertys[Pro_reg], (string)lngUI[lngText.ToLower()]);
}
}
}
if (IsCreateXmlItems) xmlDoc.Save("UI.xml");
}
/// <summary>
/// 获取控件属性值
/// </summary>
/// <param name="ClassInstance">控件对象</param>
/// <param name="PropertyName">属性名称</param>
public object GetPropertyValue(object ClassInstance, string PropertyName)
{
Type myType = ClassInstance.GetType();
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (myPropertyInfo != null)
return myPropertyInfo.GetValue(ClassInstance, null);
else
return "";
}
/// <summary>
/// 设置控件属性值
/// </summary>
/// <param name="ClassInstance">控件对象</param>
/// <param name="PropertyName">属性名称</param>
/// <param name="PropertyValue">属性值</param>
public void SetPropertyValue(object ClassInstance, string PropertyName, object PropertyValue)
{
Type myType = ClassInstance.GetType();
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
myPropertyInfo.SetValue(ClassInstance, PropertyValue, null);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Collections;
using System.Reflection;
namespace Rca4.libLng
{
public class ClassLng
{
private bool IsCreateXmlItems = true;
private static string _lngCode = "";
private static Hashtable lngUI = null;
private static Hashtable lngMsg = null;
private string _LanguagePath;
private string _Prefix;
private string _Default;
XmlDocument xmlDoc;
XmlDocument xmlMsgDoc;
/// <summary>
/// 语言对象初始化
/// </summary>
/// <param name="LanguagePath">语言包目录</param>
/// <param name="Prefix">语言包文件前缀</param>
/// <param name="Default">默认语言标识</param>
public ClassLng(string LanguagePath, string Prefix,string Default)
{
_LanguagePath = LanguagePath;
_Prefix = Prefix;
_Default = Default;
if (IsCreateXmlItems)
{
string xmlData = @"<?xml version='1.0' encoding='utf-8' ?>
<Resource>
<Messages>
</Messages>
<Items>
</Items>
</Resource>";
xmlDoc = new XmlDocument();
xmlDoc.Load(new StringReader(xmlData));
xmlMsgDoc = new XmlDocument();
FileInfo fi = new FileInfo("Msg.xml");
if (!fi.Exists)
{
xmlMsgDoc.Load(new StringReader(xmlData));
}
else
{
XmlReader reader = new XmlTextReader("Msg.xml");
xmlMsgDoc.Load(reader);
}
}
}
public string lngCode
{
get
{
return _lngCode;
}
set
{
if (_lngCode != value)
{
_lngCode = value;
this.ReadResource();
}
}
}
/// <summary>
/// 读取语言消息
/// </summary>
/// <param name="Name">语言消息名称</param>
/// <param name="Default">默认值</param>
public string Read(string Name)
{
if (IsCreateXmlItems)
{
//加入一个元素
bool IsNotFind = true;
XmlNodeList nodeList = xmlMsgDoc.SelectSingleNode("Resource/Messages").ChildNodes;
foreach(XmlNode node in nodeList)
{
if (node.Attributes["key"].InnerText == Name.ToLower())
{
IsNotFind = false;
break;
}
}
if (IsNotFind)
{
XmlNode NodeItems = xmlMsgDoc.SelectSingleNode("Resource/Messages");
XmlElement xeitem = xmlMsgDoc.CreateElement("Message");//创建一个<Item>节点
xeitem.SetAttribute("key", Name.ToLower());//设置该节点key属性
xeitem.InnerText = Name;
NodeItems.AppendChild(xeitem);
xmlMsgDoc.Save("Msg.xml");
}
}
if (lngMsg.Contains(Name.ToLower()))
{
return (string)lngMsg[Name.ToLower()];
}
else
{
return Name;
}
}
/// <summary>
/// 读取语言包
/// </summary>
private void ReadResource()
{
lngUI = new Hashtable();
lngMsg = new Hashtable();
XmlReader reader = null;
FileInfo fi = new FileInfo(_LanguagePath + "\\" + _Prefix + _lngCode + ".xml");
if (!fi.Exists)
reader = new XmlTextReader(_LanguagePath + "\\" + _Prefix + _Default + ".xml");
else
reader = new XmlTextReader(_LanguagePath + "\\" + _Prefix + _lngCode + ".xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode root = doc.DocumentElement;
XmlNodeList nodelist1 = root.SelectNodes("Items/Item");
foreach (XmlNode node in nodelist1)
{
try
{
XmlNode node1 = node.SelectSingleNode("@key");
if (node1 != null)
{
lngUI.Add(node1.InnerText.ToLower(), node.InnerText);
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
}
}
//消息语言
XmlNodeList nodelist2 = root.SelectNodes("Messages/Message");
foreach (XmlNode node in nodelist2)
{
try
{
XmlNode node1 = node.SelectSingleNode("@key");
if (node1 != null)
{
lngMsg.Add(node1.InnerText.ToLower(), node.InnerText);
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
}
}
reader.Close();
}
/// <summary>
/// 更改语言
/// </summary>
/// <param name="form">要更改语言的窗口</param>
public void ChangeLngNames(Form form)
{
string[] Propertys = { "Text", "Caption"};
int Pro_reg;
string lngText="";
if (IsCreateXmlItems)
{
//加入一个元素
XmlNode NodeItemsFrm = xmlDoc.SelectSingleNode("Resource/Items");
XmlElement xeitemFrm = xmlDoc.CreateElement("Item");//创建一个<Item>节点
xeitemFrm.SetAttribute("key", form.Text.ToLower());//设置该节点key属性
xeitemFrm.InnerText = form.Text;
NodeItemsFrm.AppendChild(xeitemFrm);
}
if (lngUI.Contains(form.Text.ToLower()))
{
form.Text = (string)lngUI[form.Text.ToLower()];
}
System.Reflection.FieldInfo[] fieldInfo = form.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
for (int i = 0; i < fieldInfo.Length; i++)
{
for (Pro_reg=0; Pro_reg < Propertys.Length; Pro_reg++)
{
lngText = GetPropertyValue(fieldInfo[i].GetValue(form), Propertys[Pro_reg]).ToString();
if (lngText != "")
{
break;
}
}
if (lngText != "")
{
if (lngUI.Contains(lngText.ToLower()))
{
if (IsCreateXmlItems)
{
XmlNode NodeItems = xmlDoc.SelectSingleNode("Resource/Items");
XmlElement xeitem = xmlDoc.CreateElement("Item");//创建一个<Item>节点
xeitem.SetAttribute("key", lngText.ToLower());//设置该节点key属性
xeitem.InnerText = lngText;
NodeItems.AppendChild(xeitem);
}
//设置语言值
SetPropertyValue(fieldInfo[i].GetValue(form), Propertys[Pro_reg], (string)lngUI[lngText.ToLower()]);
}
}
}
if (IsCreateXmlItems) xmlDoc.Save("UI.xml");
}
/// <summary>
/// 获取控件属性值
/// </summary>
/// <param name="ClassInstance">控件对象</param>
/// <param name="PropertyName">属性名称</param>
public object GetPropertyValue(object ClassInstance, string PropertyName)
{
Type myType = ClassInstance.GetType();
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (myPropertyInfo != null)
return myPropertyInfo.GetValue(ClassInstance, null);
else
return "";
}
/// <summary>
/// 设置控件属性值
/// </summary>
/// <param name="ClassInstance">控件对象</param>
/// <param name="PropertyName">属性名称</param>
/// <param name="PropertyValue">属性值</param>
public void SetPropertyValue(object ClassInstance, string PropertyName, object PropertyValue)
{
Type myType = ClassInstance.GetType();
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
myPropertyInfo.SetValue(ClassInstance, PropertyValue, null);
}
}
}