标签:
using System; using System.Collections.Generic; using System.Xml; using System.Text.RegularExpressions; class main { private delegate void Test(); //http://blog.csdn.net/tiemufeng1122/article/details/6723764 /******测试xml的读写*/ static void Main(string[] args) { #region 写一个xml ///接口:CreateXmlDeclaration /// AppendChild /// CreateElement /// SetAttribute /// Save XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDecl; xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null); xmlDoc.AppendChild(xmlDecl); string head_1 = "test1"; string head_2 = "test2"; string head_3 = "test3"; XmlElement xmlelem; xmlelem = xmlDoc.CreateElement("Test"); xmlDoc.AppendChild(xmlelem); XmlNode root = xmlDoc.SelectSingleNode("Test"); for (int i = 0; i < 5; ++i) { XmlElement xel = xmlDoc.CreateElement("Node"); string sss = i.ToString() + "a"; xel.SetAttribute(head_1, sss); sss = i.ToString() + "b"; xel.SetAttribute(head_2, sss); sss = i.ToString() + "c"; xel.SetAttribute(head_3, sss); root.AppendChild(xel); } for (int i = 0; i < 5; ++i) { XmlElement xel = xmlDoc.CreateElement("Prop"); string sss = i.ToString() + "a2"; xel.SetAttribute(head_1, sss); sss = i.ToString() + "b2"; xel.SetAttribute(head_2, sss); sss = i.ToString() + "c2"; xel.SetAttribute(head_3, sss); root.AppendChild(xel); } xmlDoc.Save("chenfeiData.xml"); #endregion //读xml: XmlDocument readXmlDoc = new XmlDocument(); readXmlDoc.Load("chenfeiData.xml"); XmlNode xNode = readXmlDoc.SelectSingleNode("Test");// 得到根节点Test XmlNodeList xnl = xNode.ChildNodes;// 得到根节点的所有子节点 foreach (XmlNode xn1 in xnl) { if (xn1.Name == "Node")//只读取Node节点,排除Prop节点 { XmlElement xe = (XmlElement)xn1; // 将节点转换为元素,便于得到节点的属性值 Console.WriteLine(xe.GetAttribute(head_1).ToString() + " " + xe.GetAttribute(head_2).ToString() + " " + xe.GetAttribute(head_3).ToString()); } } } }
标签:
原文地址:http://www.cnblogs.com/sun-shadow/p/5407851.html