XML 处理

来源:互联网 发布:c语言数据类型有哪些 编辑:程序博客网 时间:2024/05/21 22:55
 private readonly Dictionary<string, string> datafeedConfig = new Dictionary<string, string>();


        public string GetConfig(string itemName, string attributeName)
        {
            string key = itemName + "_" + attributeName;
            if (datafeedConfig.ContainsKey(key))
            {
                return datafeedConfig[key];
            }
            string configPath = AppDomain.CurrentDomain.BaseDirectory + "\\ProductDatafeedConfig\\ProductDatafeed.config";
            var xmlDocument = new XmlDocument();
            xmlDocument.Load(configPath);
            XmlNode root = xmlDocument.SelectSingleNode("ProductDatafeed");
            if (root != null)
            {
                var selectSingleNode = root.SelectSingleNode(itemName);
                if (selectSingleNode != null)
                {
                    if (selectSingleNode.Attributes != null)
                    {
                        XmlAttribute item = selectSingleNode.Attributes[attributeName];
                        if (item != null)
                        {
                            datafeedConfig.Add(key, item.Value);
                            return item.Value;
                        }
                        throw new ApplicationException("未在" + configPath + "找到指定的配置项");
                    }
                }
            }
            throw new ApplicationException(configPath + "配置错误");
        }
原创粉丝点击