开发中经常使用xml ,转一篇XML相关的文章,当作学习之用。

    程序简单的实现创建一个XML 和 查找XML节点  

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.Xml;
06  
07 namespace JackYong.XmlProxy
08 {
09      public class Program
10      {
11           static void Main(string[] args)
12           {
13                XmlProxy dom = new XmlProxy();
14                XmlNode node = dom.AddRoot("elong", "gb2312");
15                // XmlNode ordernode = dom.AddNode(node, "state", Convert.ToString((int)OrderType.pay));
16                //单独添加子节点
17                XmlNode ordernode = dom.AddNode(node, "state", null);
18                //加上乘机人数
19                XmlNode firstperson = dom.AddNode(ordernode, "orderid", "42693243423");
20                dom.AddAttribute(firstperson, "name", "jackyong");
21                XmlNode lastperson = dom.AddNode(ordernode, "orderid", "42693243423");
22                dom.AddAttribute(lastperson, "name", "jackson");
23                Console.WriteLine(dom.ToString());
24                Console.WriteLine("-------------------------------------------------------------------------");
25                XmlProxy proxy = new XmlProxy();
26                proxy.LoadXml(dom.ToString());
27                XmlProxy order = proxy.FindObject("orderid");
28                while (order != null)
29                {
30                     Console.WriteLine(order.Node.InnerXml);
31                    
32                     order = proxy.NextXmlObject();
33                }
34  
35  
36           }
37  
38      }
39      public enum OrderType
40      {
41           pay = 1,
42           deferment = 2
43  
44      }
45 }

  xmlhelper

 

001 using System;
002 using System.Collections.Generic;
003 using System.Linq;
004 using System.Text;
005 using System.Xml;
006  
007 namespace JackYong.XmlProxy
008 {
009      public class XmlProxy
010      {
011           private XmlDocument m_XmlDoc;
012           private XmlNodeList m_Nodes;
013           private int m_CurrentIndex;
014  
015           public XmlNode Node
016           {
017                get
018                {
019                     return m_XmlDoc.DocumentElement;
020                }
021           }
022  
023           public XmlProxy()
024           {
025                m_XmlDoc = new XmlDocument();
026           }
027  
028           /// <summary>
029           /// 从文件取出xml.
030           /// </summary>
031           /// <param name="filename"></param>
032           public void Load(string filename)
033           {
034                m_XmlDoc.Load(filename);
035           }
036  
037           /// <summary>
038           /// 从字符串取出xml。
039           /// </summary>
040           /// <param name="xml"></param>
041           public void LoadXml(string xml)
042           {
043                m_XmlDoc.LoadXml(xml);
044           }
045  
046           /// <summary>
047           /// 找到特定的节点值
048           /// </summary>
049           /// <param name="name">节点名</param>
050           /// <param name="values">节点值,可以为null即不匹配节点值</param>
051           /// <returns>返回值</returns>
052           public string Find(string name, string values)
053           {
054                m_CurrentIndex = 0;
055                string xpath = "//" + name;
056                if (values != null && values != "")
057                {
058                     xpath += "[text()=\"" + values + "\"]";
059                }
060                m_Nodes = m_XmlDoc.SelectNodes(xpath);
061                return Next();
062           }
063  
064           /// <summary>
065           /// 找到特定的节点
066           /// </summary>
067           /// <param name="name">节点名</param>
068           /// <returns>返回值</returns>
069           public string Find(string name)
070           {
071                return Find(name, null);
072           }
073  
074           /// <summary>
075           /// 找到特定的节点
076           /// </summary>
077           /// <param name="name">节点名</param>
078           /// <param name="values">节点值,可以为null即不匹配节点值</param>
079           /// <returns>返回值</returns>
080           public XmlProxy FindObject(string name, string values)
081           {
082                m_CurrentIndex = 0;
083                string xpath = "//" + name;
084                if (values != null && values != "")
085                {
086                     xpath += "[text()=\"" + values + "\"]";
087                }
088                m_Nodes = m_XmlDoc.SelectNodes(xpath);
089                return NextXmlObject();
090           }
091  
092           /// <summary>
093           /// 找到特定的节点
094           /// </summary>
095           /// <param name="name">节点名</param>
096           /// <returns>返回值</returns>
097           public XmlProxy FindObject(string name)
098           {
099                return FindObject(name, null);
100           }
101  
102           /// <summary>
103           /// 返回下一个节点值
104           /// </summary>
105           /// <returns>返回值</returns>
106           public string Next()
107           {
108                if (m_Nodes == null)
109                     throw new XmlException("No more nodes");
110                if (m_Nodes.Count - 1 < m_CurrentIndex)
111                     return null;
112                return m_Nodes[m_CurrentIndex++].InnerXml;
113           }
114  
115           /// <summary>
116           /// 此次查询是否有子节点
117           /// </summary>
118           /// <returns></returns>
119           public bool HasChild()
120           {
121                if (m_Nodes == null)
122                     throw new XmlException("No children.");
123                if (m_Nodes.Count > 0)
124                     return m_Nodes[0].HasChildNodes;
125                return false;
126           }
127  
128           /// <summary>
129           /// 查找个数
130           /// </summary>
131           public int Count
132           {
133                get
134                {
135                     if (m_Nodes == null)
136                          throw new XmlException("No node.");
137                     return m_Nodes.Count;
138                }
139           }
140  
141           /// <summary>
142           /// 返回一个xml对象,可以进行二次查找
143           /// </summary>
144           /// <returns></returns>
145           public XmlProxy NextXmlObject()
146           {
147                if (m_Nodes == null)
148                     throw new XmlException("No more nodes.");
149                if (m_Nodes.Count - 1 < m_CurrentIndex)
150                     return null;
151  
152                XmlProxy parser = new XmlProxy();
153                parser.LoadXml(m_Nodes[m_CurrentIndex++].OuterXml);
154                return parser;
155           }
156  
157           /// <summary>
158           /// 添加子节点
159           /// </summary>
160           /// <param name="node"></param>
161           /// <param name="name"></param>
162           /// <param name="values">null则只添加节点名</param>
163           /// <returns></returns>
164           public XmlNode AddNode(XmlNode node, string name, string values)
165           {
166                if (node == null || name == null)
167                     return null;
168                XmlNode cn = m_XmlDoc.CreateNode(XmlNodeType.Element, name, "");
169                node.AppendChild(cn);
170                if (values != null && values != "")
171                {
172                     XmlNode tn = m_XmlDoc.CreateTextNode(values);
173                     cn.AppendChild(tn);
174                }
175                return cn;
176           }
177  
178           public void AddAttribute(XmlNode node, string name, string value)
179           {
180                if (node == null || name == null)
181                     return;
182                XmlAttribute attr = m_XmlDoc.CreateAttribute(name);
183                attr.Value = value;
184                if (node.Attributes != null) node.Attributes.Append(attr);
185           }
186  
187           /// <summary>
188           /// 添加根节点
189           /// </summary>
190           /// <param name="name"></param>
191           /// <returns></returns>
192           public XmlNode AddRoot(string name, string encode)
193           {
194                string xml = "<?xml version=\"1.0\" encoding=\"" + encode + "\" ?>";
195                xml += "<" + name + "></" + name + ">";
196                m_XmlDoc.LoadXml(xml);
197                return m_XmlDoc.DocumentElement;
198           }
199  
200           /// <summary>
201           /// 输出成字符串
202           /// </summary>
203           /// <returns></returns>
204           public override string ToString()
205           {
206                return m_XmlDoc.OuterXml;
207           }
208  
209           public string GetAttributeValue(string name)
210           {
211                XmlNode root = m_XmlDoc.DocumentElement;
212                if (root.Attributes == null || root.Attributes.Count < 1)
213                     return null;
214  
215                XmlAttribute attr = root.Attributes[name];
216                if (attr == null)
217                     return null;
218  
219                return attr.Value;
220           }
221      }
222 }
 posted on 2011-01-26 14:52  dongpo  阅读(382)  评论(0编辑  收藏  举报