【原】Xml Helper
开发中经常使用xml ,自己随便写了一个 xml helper 很久了,拿出来晒晒。
程序简单的实现创建一个XML 和 查找XML节点
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace JackYong.XmlProxy { public class Program { static void Main(string[] args) { XmlProxy dom = new XmlProxy(); XmlNode node = dom.AddRoot("elong", "gb2312"); // XmlNode ordernode = dom.AddNode(node, "state", Convert.ToString((int)OrderType.pay)); //单独添加子节点 XmlNode ordernode = dom.AddNode(node, "state", null); //加上乘机人数 XmlNode firstperson = dom.AddNode(ordernode, "orderid", "42693243423"); dom.AddAttribute(firstperson, "name", "jackyong"); XmlNode lastperson = dom.AddNode(ordernode, "orderid", "42693243423"); dom.AddAttribute(lastperson, "name", "jackson"); Console.WriteLine(dom.ToString()); Console.WriteLine("-------------------------------------------------------------------------"); XmlProxy proxy = new XmlProxy(); proxy.LoadXml(dom.ToString()); XmlProxy order = proxy.FindObject("orderid"); while (order != null) { Console.WriteLine(order.Node.InnerXml); order = proxy.NextXmlObject(); } } } public enum OrderType { pay = 1, deferment = 2 } }
xmlhelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace JackYong.XmlProxy { public class XmlProxy { private XmlDocument m_XmlDoc; private XmlNodeList m_Nodes; private int m_CurrentIndex; public XmlNode Node { get { return m_XmlDoc.DocumentElement; } } public XmlProxy() { m_XmlDoc = new XmlDocument(); } /// <summary> /// 从文件取出xml. /// </summary> /// <param name="filename"></param> public void Load(string filename) { m_XmlDoc.Load(filename); } /// <summary> /// 从字符串取出xml。 /// </summary> /// <param name="xml"></param> public void LoadXml(string xml) { m_XmlDoc.LoadXml(xml); } /// <summary> /// 找到特定的节点值 /// </summary> /// <param name="name">节点名</param> /// <param name="values">节点值,可以为null即不匹配节点值</param> /// <returns>返回值</returns> public string Find(string name, string values) { m_CurrentIndex = 0; string xpath = "//" + name; if (values != null && values != "") { xpath += "[text()=\"" + values + "\"]"; } m_Nodes = m_XmlDoc.SelectNodes(xpath); return Next(); } /// <summary> /// 找到特定的节点 /// </summary> /// <param name="name">节点名</param> /// <returns>返回值</returns> public string Find(string name) { return Find(name, null); } /// <summary> /// 找到特定的节点 /// </summary> /// <param name="name">节点名</param> /// <param name="values">节点值,可以为null即不匹配节点值</param> /// <returns>返回值</returns> public XmlProxy FindObject(string name, string values) { m_CurrentIndex = 0; string xpath = "//" + name; if (values != null && values != "") { xpath += "[text()=\"" + values + "\"]"; } m_Nodes = m_XmlDoc.SelectNodes(xpath); return NextXmlObject(); } /// <summary> /// 找到特定的节点 /// </summary> /// <param name="name">节点名</param> /// <returns>返回值</returns> public XmlProxy FindObject(string name) { return FindObject(name, null); } /// <summary> /// 返回下一个节点值 /// </summary> /// <returns>返回值</returns> public string Next() { if (m_Nodes == null) throw new XmlException("No more nodes"); if (m_Nodes.Count - 1 < m_CurrentIndex) return null; return m_Nodes[m_CurrentIndex++].InnerXml; } /// <summary> /// 此次查询是否有子节点 /// </summary> /// <returns></returns> public bool HasChild() { if (m_Nodes == null) throw new XmlException("No children."); if (m_Nodes.Count > 0) return m_Nodes[0].HasChildNodes; return false; } /// <summary> /// 查找个数 /// </summary> public int Count { get { if (m_Nodes == null) throw new XmlException("No node."); return m_Nodes.Count; } } /// <summary> /// 返回一个xml对象,可以进行二次查找 /// </summary> /// <returns></returns> public XmlProxy NextXmlObject() { if (m_Nodes == null) throw new XmlException("No more nodes."); if (m_Nodes.Count - 1 < m_CurrentIndex) return null; XmlProxy parser = new XmlProxy(); parser.LoadXml(m_Nodes[m_CurrentIndex++].OuterXml); return parser; } /// <summary> /// 添加子节点 /// </summary> /// <param name="node"></param> /// <param name="name"></param> /// <param name="values">null则只添加节点名</param> /// <returns></returns> public XmlNode AddNode(XmlNode node, string name, string values) { if (node == null || name == null) return null; XmlNode cn = m_XmlDoc.CreateNode(XmlNodeType.Element, name, ""); node.AppendChild(cn); if (values != null && values != "") { XmlNode tn = m_XmlDoc.CreateTextNode(values); cn.AppendChild(tn); } return cn; } public void AddAttribute(XmlNode node, string name, string value) { if (node == null || name == null) return; XmlAttribute attr = m_XmlDoc.CreateAttribute(name); attr.Value = value; if (node.Attributes != null) node.Attributes.Append(attr); } /// <summary> /// 添加根节点 /// </summary> /// <param name="name"></param> /// <returns></returns> public XmlNode AddRoot(string name, string encode) { string xml = "<?xml version=\"1.0\" encoding=\"" + encode + "\" ?>"; xml += "<" + name + "></" + name + ">"; m_XmlDoc.LoadXml(xml); return m_XmlDoc.DocumentElement; } /// <summary> /// 输出成字符串 /// </summary> /// <returns></returns> public override string ToString() { return m_XmlDoc.OuterXml; } public string GetAttributeValue(string name) { XmlNode root = m_XmlDoc.DocumentElement; if (root.Attributes == null || root.Attributes.Count < 1) return null; XmlAttribute attr = root.Attributes[name]; if (attr == null) return null; return attr.Value; } } }
出处:http://www.cnblogs.com/liuyong/
作者喜欢研究 Sql Server ,ASP.NET MVC , Jquery WCF 等技术,同时关心分布式架构的设计应用。转载请保留原文链接,谢谢!