读取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;
            }


        }

    }

下面是对应的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>
posted @ 2007-04-03 11:46  Adam.Zhao  阅读(786)  评论(0编辑  收藏  举报