void XmlBase() { XmlDocument xml = new XmlDocument();//初始化一个xml文档 xml.Load("../../test.xml");//从指定的URL加载xml文档 xml.LoadXml("<root>name</root>");//从指定的xml文档中的字符串加载xml文档 XmlNode root = xml.SelectSingleNode("/root");//获取一个节点 XmlNodeList nodeList = xml.SelectNodes("/root/news");//获取同名同级节点集合 XmlNodeList childList = root.ChildNodes;//获取指定节点下所有子节点 bool b = root.HasChildNodes;//判断节点下是否有子节点 XmlElement newNode = xml.CreateElement("news");//创建一个新节点 root.AppendChild(newNode);//将节点插入指定节点下,作为其子节点 newNode.SetAttribute("id","111");//为指定节点的新建属性赋值 string id = newNode.Attributes["id"].Value;//获取指定节点的属性值 string content = newNode.InnerText;//获取或设置指定节点的文本 xml.Save("../../test.xml");//保存XML文档 }
#region 01.创建XML文档-static void CreatXml() ///<summary> ///创建XML文档 ///</summary> static void CreatXml() { XmlDocument xDoc = new XmlDocument(); //加入XML的声明段落,<?xmlversion="1.0" encoding="gb2312"?> XmlDeclaration xDec = xDoc.CreateXmlDeclaration("1.0","gb2312", null); xDoc.AppendChild(xDec); //加入根元素 XmlElement xElem = xDoc.CreateElement("","Employees", ""); xDoc.AppendChild(xElem); //加入子元素 for (int i = 1; i < 3; i++) { XmlNode xNode = xDoc.SelectSingleNode("Employees");//查找<Employees> XmlElement xElemC = xDoc.CreateElement("Node");//创建一个<Node>节点 xElemC.SetAttribute("genre","李赞红");//设置该节点genre属性 xElemC.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性 XmlElement xesub1 = xDoc.CreateElement("title");//创建一个<title>节点 xesub1.InnerText = "CS从入门到精通";//设置节点 文本 xElemC.AppendChild(xesub1);//添加到<Node>节点中下 XmlElement xesub2 = xDoc.CreateElement("author"); xesub2.InnerText = "候捷"; xElemC.AppendChild(xesub2); XmlElement xesub3 = xDoc.CreateElement("price"); xesub3.InnerText = "58.3"; xElemC.AppendChild(xesub3); xNode.AppendChild(xElemC);//添加到<Employees>节点中 } //保存创建好的XML文档 xDoc.Save("../../test.xml"); } #endregion
#region 02.添加节点- static voidAddNode() ///<summary> ///添加节点 ///</summary> static void AddNode() { XmlDocument xDoc = new XmlDocument(); xDoc.Load("../../test.xml");//载入要操作的xml文档 XmlNode xNode = xDoc.SelectSingleNode("Employees");//查找<Employees>节点 XmlElement xElemC = xDoc.CreateElement("Node");//创建一个新的<node>节点 xElemC.SetAttribute("genre","张三");//为<node>设置属性值 xElemC.SetAttribute("ISBN","1-1111-1"); XmlElement xesub1 = xDoc.CreateElement("title");//创建一个新的<title>节点 xesub1.InnerXml = "C#入门帮助";//设置节点内容 xElemC.AppendChild(xesub1);//将<title>节点添加到<node>下 XmlElement xesub2 = xDoc.CreateElement("author"); xesub2.InnerText = "高手"; xElemC.AppendChild(xesub2); XmlElement xesub3 = xDoc.CreateElement("price"); xesub3.InnerText = "58.3"; xElemC.AppendChild(xesub3); xNode.AppendChild(xElemC);//将<node>节点添加到<Employees>节点下 xDoc.Save("../../test.xml");//保存xml文档 } #endregion
#region 03.删除节点-static voidDeleteNode() ///<summary> ///删除节点 ///</summary> static void DeleteNode() { XmlDocument xDoc = new XmlDocument(); xDoc.Load("../../test.xml"); XmlNode xNode = xDoc.SelectSingleNode("Employees"); XmlNodeList xNodeList = xNode.ChildNodes;//获取<Employees>节点下的所有子节点 foreach (XmlNode xNodeCin xNodeList) { XmlElement xElem = xNodeCas XmlElement;//转换类型 xElem.RemoveAttribute("genre");//删除genre属性 XmlNodeList exsub = xElem.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xnin exsub)//遍历 { XmlElement xe = (XmlElement)xn; if (xe.Name == "author")//如果找到<author>节点 { xElem.RemoveChild(xe);//则删除 } } } //删除genre属性值为张三的节点 for (int i = 0; i < xNodeList.Count; i++) { XmlElement xElem = (XmlElement)xNodeList.Item(i); if (xElem.GetAttribute("genre") =="张三") { xNode.RemoveChild(xElem); if (i < xNodeList.Count) i = i - 1; } } xDoc.Save("../../test.xml"); } #endregion
#region 04.修改节点-static voidModifyNode() ///<summary> ///修改节点 ///</summary> static void ModifyNode() { XmlDocument xDoc = new XmlDocument(); xDoc.Load("../../test.xml"); XmlNodeList xNodeList = xDoc.SelectSingleNode("Employees").ChildNodes; foreach (XmlNode xNodein xNodeList)//遍历<Employees>节点下的子节点 { XmlElement xElem= xNodeas XmlElement;//类型转换 XmlElement xesub = xDoc.CreateElement("flag");//创建一个新的节点 xesub.InnerXml = "1";//设置值 xElem.AppendChild(xesub); if (xElem.GetAttribute("genre") =="张三")//获取节点属性 { xElem.SetAttribute("genre","李四");//设置节点属性 } foreach (XmlNode xnin xElem)//遍历二级节点下的子节点 { XmlElement xe = xnas XmlElement; if(xe.Name=="author") { xe.InnerText = "作者"; } } } xDoc.Save("../../test.xml"); } #endregion
#region 05.读取xml文本文件-static void GetXml() ///<summary> ///读取xml文本文件 ///</summary> static void GetXml() { System.IO.StreamReader myFile =new System.IO.StreamReader("../../test.xml", System.Text.Encoding.Default); string str = myFile.ReadToEnd(); myFile.Close(); Console.Write(str); } #endregion