using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace System.Bwch.XmlConfig { /**/////// 读取XML配置文件类 /// public class XmlHelper { private string strXmlPath = ""; //Xml文档路径 private XmlDocument xmlDoc; //XML文档 /**/////// 初始化ReadXml类 /// /// XML文件路径 public XmlHelper(string XMLPath) { if (!System.IO.File.Exists(XMLPath)) { throw new Exception("没有找到指定的路径:" + XMLPath + "的XML文档"); } strXmlPath = XMLPath; xmlDoc = new XmlDocument(); xmlDoc.Load(XMLPath); } /**/////// 读取XML文件指定键值的value 值 /// /// 键值的路径,格式为(根节点/节点/子节点) /// 指定键值的属性名称 /// value值 public string ReadXmlValue(string XMLNodePath,string valueName) { try { XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XMLNodePath); return xml.GetAttribute(valueName); } catch (Exception ex) { throw new Exception(ex.Message); } } /**/////// 写XML指定节点的属性 /// /// 键值路径,格式为((根节点/节点/子节点)) /// 属性名称 /// 属性 /// public bool WriteXmlValue(string XmlNodePath,string valueName, string Value) { try { XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath); xml.SetAttribute(valueName, Value); //设置 xmlDoc.Save(strXmlPath); //保存 return true; } catch (Exception ex) { throw new Exception(ex.Message); } } /**/////// 读取XML键值 /// /// 键值路径,格式为((根节点/节点/子节点)) /// public string ReadXmlKey(string XmlNodePath) { try { XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath); return xml.InnerText; } catch (Exception ex) { throw new Exception(ex.Message); } } /**/////// 写XML键值 /// /// 键值路径,格式为((根节点/节点/子节点)) /// 值 /// public bool WriteXmlKey(string XmlNodePath, string Value) { try { XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath); xml.InnerText=Value; xmlDoc.Save(strXmlPath); return true; } catch (Exception ex) { throw new Exception(ex.Message); } } } }
读取 XML类 (XmlHelper)
最新推荐文章于 2019-05-20 09:13:00 发布