C#讀取xml配置文件


 
一、配置xml
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <Section Name="system">
    <Key Name="srcPath" value="E:\EDIFileTest\EDIPackageA" />    
    <Key Name="tarPath" value="E:\EDIFileTest\EDIPackageB" />
    <Key Name="savePath" value="E:\EDIFileTest\Test.csv" />
  </Section>
</configuration>
View Code

 

Section的Name值可以自定義,可以有多個Section節點;
Section節點下的Key節點用來記錄Name、Value值,程序根據Name取Value值
 
二、xml幫助類
 
/// <summary>
        /// 獲取XML配置文件的值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string getValue(string section, string key)
        {
            string retValue = string.Empty;
            XmlDocument xd = new XmlDocument();
            xd.Load(configlFile);
            XmlElement xe = xd.DocumentElement;
            if (xe.Name == "configuration")
            {
                 //根據固定的文檔結構查找指定配置項
                foreach (System.Xml.XmlLinkedNode xcea in xe.ChildNodes)
                {
                    if (xcea.NodeType != System.Xml.XmlNodeType.Element)  //如果不是元素,繼續
                    {
                        continue;
                    }
                    System.Xml.XmlElement xce = (System.Xml.XmlElement)xcea;
                    if ((xce.NodeType == System.Xml.XmlNodeType.Element) && (xce.HasAttributes) && (xce.Name=="Section" ))
                    {
                        if (xce.Attributes[0].Name == "Name" && xce.Attributes[0].Value == section)  // 通過屬性判斷當前元素是否是需要查找的配置項
                        {
                            foreach (System.Xml.XmlLinkedNode xccea in xce.ChildNodes)
                            {
                                if (xccea.NodeType != System.Xml.XmlNodeType.Element)  //如果不是元素,繼續
                                {
                                    continue;
                                }
                               
                                System.Xml.XmlElement xcce = (System.Xml.XmlElement)xccea;
                                if (xcce.NodeType == System.Xml.XmlNodeType.Element  && xcce.HasAttributes && xcce.Name == "Key")
                                {
                                    if (xcce.Attributes[0].Name == "Name" && xcce.Attributes[0].Value == key)
                                    {
                                        if (string.IsNullOrEmpty(retValue))
                                        {
                                            retValue = xcce.Attributes[1].Value;
                                        }
                                        else
                                        {
                                            retValue = retValue + "," +  xcce.Attributes[1].Value;
                                        }
                                       
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return removeRightComma(retValue);
        }
View Code

 

三、程序調用

string srcPath = XMLConfig.getValue("system", "srcPath");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM