• 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");

  • 相关阅读:
    lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
    lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
    lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
    lintcode:递归打印数字
    lintcode :Segmemt Tree Build II
    lintcode 中等题:find the missing number 寻找缺失的数
    lintcode 中等题: Implement Trie
    lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
    lintcode 中等题:Palindrome Linked List 回文链表
    [LeetCode] Merge Sorted Array
  • 原文地址:https://www.cnblogs.com/zyl1994/p/10154210.html
Copyright © 2020-2023  润新知