读取XML文件内容的简单方法
读取XML文件内容的简单方法:
下面是对应的XML文件:
/// <summary>
/// XmlOperation 的摘要说明。
/// </summary>
public sealed class XmlOperation
{
//private static string configFile = System.IO.Path.GetFullPath("..\\..\\Xml\\Words.xml");
private static string configFile = System.IO.Path.GetFullPath("Xml\\Words.xml");
/// <summary>
/// 获取字符拼音名称
/// </summary>
/// <param name="database">汉字</param>
/// <returns>真实库名</returns>
public static string GetWordPYName(string word)
{
XmlDocument xml = new XmlDocument();
try
{
xml.Load(configFile);
XmlElement root = xml.DocumentElement;
string xpath = "descendant::word[@cname='" + word + "']";
XmlNode dbNode = root.SelectSingleNode(xpath);
string pyName = "";
if (dbNode != null)
{
pyName = dbNode.Attributes["pyname"].Value;
}
else
{
pyName = word;
}
return pyName;
}
catch (Exception ex)
{
return word;
}
}
}
/// XmlOperation 的摘要说明。
/// </summary>
public sealed class XmlOperation
{
//private static string configFile = System.IO.Path.GetFullPath("..\\..\\Xml\\Words.xml");
private static string configFile = System.IO.Path.GetFullPath("Xml\\Words.xml");
/// <summary>
/// 获取字符拼音名称
/// </summary>
/// <param name="database">汉字</param>
/// <returns>真实库名</returns>
public static string GetWordPYName(string word)
{
XmlDocument xml = new XmlDocument();
try
{
xml.Load(configFile);
XmlElement root = xml.DocumentElement;
string xpath = "descendant::word[@cname='" + word + "']";
XmlNode dbNode = root.SelectSingleNode(xpath);
string pyName = "";
if (dbNode != null)
{
pyName = dbNode.Attributes["pyname"].Value;
}
else
{
pyName = word;
}
return pyName;
}
catch (Exception ex)
{
return word;
}
}
}
下面是对应的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<words>
<word cname="单" pyname="shan" />
<word cname="朴" pyname="piao" />
<word cname="石" pyname="shi" />
<word cname="乐" pyname="yue" />
<word cname="召" pyname="shao" />
<word cname="贲" pyname="ben" />
<word cname="隗" pyname="kui" />
<word cname="区" pyname="ou" />
<word cname="长" pyname="zhang" />
<word cname="仇" pyname="qiu" />
<word cname="喘" pyname="xie" />
<word cname="尉" pyname="yi" />
</words>
<words>
<word cname="单" pyname="shan" />
<word cname="朴" pyname="piao" />
<word cname="石" pyname="shi" />
<word cname="乐" pyname="yue" />
<word cname="召" pyname="shao" />
<word cname="贲" pyname="ben" />
<word cname="隗" pyname="kui" />
<word cname="区" pyname="ou" />
<word cname="长" pyname="zhang" />
<word cname="仇" pyname="qiu" />
<word cname="喘" pyname="xie" />
<word cname="尉" pyname="yi" />
</words>