zoukankan      html  css  js  c++  java
  • 读取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>
  • 相关阅读:
    Xcode7 真机调试步骤以及遇到的问题解决办法
    AndroidStudio .gitinore编写
    Android Studio Jar、so、library项目依赖
    Android studio导入eclipse项目混淆打包出错
    spring中ref属性与<ref/>标签
    NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.view.InternalResourceViewResolver' available
    Maven学习笔记
    Tomcat日志与Log4j日志
    Git笔记
    Tomcat学习
  • 原文地址:https://www.cnblogs.com/adam/p/698018.html
Copyright © 2011-2022 走看看