xml
一:如果不想有转意 则用CDATA <name><![CDATA[张三<李四>王五<99>]]></name>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 通过编程方式实现xml写入 2 ////1.在内存中构建一个Dom对象 3 //XmlDocument xmlDoc = new XmlDocument(); 4 //XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); 5 //xmlDoc.AppendChild(xmlDec); 6 ////2.为文档增加一个根元素 7 //XmlElement xmlRoot = xmlDoc.CreateElement("school"); 8 //xmlDoc.AppendChild(xmlRoot); 9 ////3.为根源素里面增加子元素,接下来将子元素添加到Root 10 //XmlElement xmlClass = xmlDoc.CreateElement("class"); 11 //XmlAttribute xmlAttr = xmlDoc.CreateAttribute("id"); 12 //xmlAttr.Value = "c01"; 13 //xmlClass.Attributes.Append(xmlAttr); 14 //xmlRoot.AppendChild(xmlClass); 15 ////4.class元素创建一个student节点 16 //XmlElement xmlStudentElement = xmlDoc.CreateElement("student"); 17 //XmlAttribute sttrSid = xmlDoc.CreateAttribute("sid"); 18 //sttrSid.Value = "s001"; 19 //xmlClass.AppendChild(xmlStudentElement); 20 21 ////5.向student节点中怎家一个name节点和age节点 22 //XmlElement xmlNameElement = xmlDoc.CreateElement("name"); 23 //xmlNameElement.InnerText = "黄林"; 24 //xmlStudentElement.AppendChild(xmlNameElement); 25 26 //XmlElement xmlAgeElement = xmlDoc.CreateElement("age"); 27 //xmlAgeElement.InnerText = "18"; 28 //xmlStudentElement.AppendChild(xmlAgeElement); 29 30 ////end 将Dom对象写入到xml文件中 31 32 //xmlDoc.Save("school.xml"); 33 //MessageBox.Show("ok"); 34 #endregion 35 36 #region 把List集合中的内容写入到一个xml文件中,通过XmlDocument方式写入Xml文件 37 //List<Person> list = new List<Person>(); 38 //list.Add(new Person() { Name = "黄林", Age = 19, Email = "hl@yahoo.com" }); 39 //list.Add(new Person() { Name = "许正龙", Age = 29, Email = "xzl@yahoo.com" }); 40 //list.Add(new Person() { Name = "何红卫", Age = 39, Email = "hhw@yahoo.com" }); 41 //list.Add(new Person() { Name = "杨硕", Age = 9, Email = "ys@yahoo.com" }); 42 ////1.创建一个Dom对象 43 //XmlDocument xDoc = new XmlDocument(); 44 ////2.编写文档定义 45 //XmlDeclaration xmlDec = xDoc.CreateXmlDeclaration("1.0", "utf-8", null); 46 //xDoc.AppendChild(xmlDec); 47 48 ////3.编写一个根节点 49 //XmlElement xmlRoot = xDoc.CreateElement("List"); 50 //xDoc.AppendChild(xmlRoot); 51 52 ////4循环Person节点 53 //for (int i = 0; i < list.Count; i++) { 54 // XmlElement xmlPersonElement = xDoc.CreateElement("Person"); 55 // XmlAttribute xmlAttr = xDoc.CreateAttribute("id"); 56 // xmlAttr.Value = i.ToString(); 57 // xmlPersonElement.Attributes.Append(xmlAttr); 58 // xmlRoot.AppendChild(xmlPersonElement); 59 60 // XmlElement xmlNameElement = xDoc.CreateElement("Name"); 61 // xmlNameElement.InnerText = list[i].Name.ToString(); 62 // xmlPersonElement.AppendChild(xmlNameElement); 63 64 // XmlElement xmlAgeElement = xDoc.CreateElement("Age"); 65 // xmlAgeElement.InnerText = list[i].Age.ToString(); 66 // xmlPersonElement.AppendChild(xmlAgeElement); 67 68 // XmlElement xmlEmailElement = xDoc.CreateElement("Email"); 69 // xmlEmailElement.InnerText = list[i].Email.ToString(); 70 // xmlPersonElement.AppendChild(xmlEmailElement); 71 //} 72 //xDoc.Save("student.xml"); 73 //MessageBox.Show("OK"); 74 75 #endregion 76 77 78 #region 通过XDocument方式写入Xml文件 79 80 List<Person> list = new List<Person>(); 81 list.Add(new Person() { Name = "黄林", Age = 19, Email = "hl@yahoo.com" }); 82 list.Add(new Person() { Name = "许正龙", Age = 29, Email = "xzl@yahoo.com" }); 83 list.Add(new Person() { Name = "何红卫", Age = 39, Email = "hhw@yahoo.com" }); 84 list.Add(new Person() { Name = "杨硕", Age = 9, Email = "ys@yahoo.com" }); 85 86 //1.创建一个Dom对象 87 XDocument xDoc = new XDocument(); 88 XDeclaration xDec = new XDeclaration("1.0", "utf-8", "no"); 89 90 //====================================================== 91 //设置xml的文档定义 92 xDoc.Declaration = xDec; 93 //====================================================== 94 95 96 //2.创建根节点 97 XElement rootElement = new XElement("List"); 98 xDoc.Add(rootElement); 99 100 101 //3.循环List集合创建Person节点 102 for (int i = 0; i < list.Count; i++) { 103 //为每个Person对象创建一个Person元素 104 XElement xElementPerson = new XElement("Person"); 105 xElementPerson.SetAttributeValue("id", (i + 1).ToString()); 106 107 xElementPerson.SetElementValue("Name", list[i].Name); 108 xElementPerson.SetElementValue("Age", list[i].Age); 109 xElementPerson.SetElementValue("Email", list[i].Email); 110 rootElement.Add(xElementPerson); 111 } 112 113 //4.保存到文件 114 xDoc.Save("ListNew.xml"); 115 MessageBox.Show("ok"); 116 #endregion
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 读取xml 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnReadXml_Click(object sender, EventArgs e) { 7 8 #region 通过XDocument将xml文件递归加载到Treeview中 9 ////1.读取xml文件 10 //XDocument document = XDocument.Load("student.xml"); 11 ////2.获取节点 12 //XElement rootElement = document.Root; 13 ////3.将xml的根元素加载到TreeView的根节点上 14 //TreeNode rootName = treeView1.Nodes.Add(rootElement.Name.ToString()); 15 16 ////4.递归加载 17 //LoadXmlToTreeView(rootElement, rootName.Nodes); 18 #endregion 19 20 #region 通过XmlDocument将xml文件递归加载到Treeview中 21 //1加载Xml文件到对象 22 XmlDocument document = new XmlDocument(); 23 document.Load("student.xml"); 24 //获取根节点 25 XmlElement rootElement = document.DocumentElement; 26 TreeNode rootNode = treeView1.Nodes.Add(rootElement.Name); 27 28 //实现递归将xml加载到TreeView中 29 30 LoadToTreeByXmlDocument(rootElement, rootNode.Nodes); 31 #endregion 32 } 33 /// <summary> 34 /// 通过XmlDocument的方式将xml元素递归加载到TreeView上 35 /// </summary> 36 /// <param name="rootElement"></param> 37 /// <param name="treeNodeCollection"></param> 38 private void LoadToTreeByXmlDocument(XmlElement rootElement, TreeNodeCollection treeNodeCollection) { 39 foreach (XmlNode item in rootElement.ChildNodes) { 40 // 在继续之前需要判断一下当前节点是什么类型的节点 41 if (item.NodeType==XmlNodeType.Element) { 42 //如果当前节点是一个元素节点,则把当前节点加载到treeView中 43 TreeNode node = treeNodeCollection.Add(item.Name); 44 //递归调用 45 46 } 47 } 48 } 49 /// <summary> 50 /// 通过XDocument的方式将xml元素递归加载到TreeView上 51 /// </summary> 52 /// <param name="rootElement"></param> 53 /// <param name="treeNodeCollection"></param> 54 private void LoadXmlToTreeView(XElement rootElement, TreeNodeCollection treeNodeCollection) { 55 //根据根元素获取所有子元素 56 foreach (XElement item in rootElement.Elements()) { 57 if (item.Elements().Count() == 0) { 58 //当前节点没有子节点,给当前节点 添加名字和 内容 59 treeNodeCollection.Add(item.Name.ToString()).Nodes.Add(item.Value); 60 61 } else { 62 TreeNode node = treeNodeCollection.Add(item.Name.ToString()); 63 LoadXmlToTreeView(item, node.Nodes); 64 } 65 66 } 67 }