记录一些常用的操作XML文档的方法,XML示例文档如下:
<?xml version="1.0" encoding="utf-8"?> <Animal> <Dog Sound="WangWang" Favor="Bone" /> <Cat Sound="MiaoMiao" Favor="Fish" /> </Animal>
1.通过XmlTextWriter创建文档:
1 public static void WriteByWriter() 2 { 3 XmlTextWriter tw = new XmlTextWriter(path,Encoding.UTF8); 4 tw.Formatting = Formatting.Indented; 5 tw.WriteStartDocument(); 6 tw.WriteStartElement("Animal"); 7 ///////// 8 tw.WriteStartElement("Dog"); 9 tw.WriteAttributeString("Sound","WangWang"); 10 tw.WriteStartAttribute("Favor"); 11 tw.WriteString("Bone"); 12 tw.WriteEndAttribute(); 13 tw.WriteEndElement(); 14 ///////// 15 tw.WriteStartElement("Cat"); 16 tw.WriteAttributeString("Sound", "MiaoMiao"); 17 tw.WriteStartAttribute("Favor"); 18 tw.WriteString("Fish"); 19 tw.WriteEndAttribute(); 20 tw.WriteEndElement(); 21 /////////// 22 tw.WriteEndElement(); 23 tw.WriteEndDocument(); 24 tw.Flush(); 25 }WriteByWrite
2.通过XmlDocument创建文档:
1 public static void WritebyXmlDoc() 2 { 3 XmlDocument doc = new XmlDocument(); 4 doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); 5 XmlElement rootNode = doc.CreateElement("Animal"); 6 doc.AppendChild(rootNode); 7 8 XmlElement subNode = doc.CreateElement("Dog"); 9 rootNode.AppendChild(subNode); 10 subNode.SetAttribute("Sound", "WangWang"); 11 subNode.SetAttribute("Favor", "Bone"); 12 13 subNode = doc.CreateElement("Cat"); 14 rootNode.AppendChild(subNode); 15 subNode.SetAttribute("Sound", "MiaoMiao"); 16 subNode.SetAttribute("Favor", "Fish"); 17 doc.Save(path); 18 }WritebyXmlDoc
3.通过字符串创建文档:
1 public static void WritebyString() 2 { 3 StringBuilder sb = new StringBuilder(); 4 sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 5 sb.Append("<Animal>"); 6 sb.Append("<Dog Sound=\"WangWang\" Favor=\"Bone\"/>"); 7 sb.Append("<Cat Sound=\"MiaoMiao\" Favor=\"Fish\"/>"); 8 sb.Append("</Animal>"); 9 XmlDocument doc = new XmlDocument(); 10 doc.LoadXml(sb.ToString()); 11 doc.Save(path); 12 }WritebyString
4.通过XmlDocument读取文档:
1 public static void ReadByXmlDoc() 2 { 3 XmlDocument doc = new XmlDocument(); 4 doc.Load(path); 5 XmlNode rootNode = doc.SelectSingleNode("Animal"); 6 XmlNodeList nodeList = rootNode.ChildNodes; 7 foreach (XmlNode node in nodeList) 8 { 9 Console.WriteLine(node.Name);//节点名称 10 Console.WriteLine(node.Attributes[0].Value);//属性值 11 Console.WriteLine(node.Attributes["Sound"].Value);//属性值 12 foreach (XmlAttribute attr in node.Attributes) 13 { 14 //获取属性名称及值 15 Console.WriteLine(attr.Name + " " + attr.Value); 16 } 17 } 18 }ReadByXmlDoc
5.通过XmlTextReader读取文档:
1 public static void ReadByReader() 2 { 3 XmlDocument doc = new XmlDocument(); 4 doc.Load(path); 5 //XmlTextReader reader = new XmlTextReader(path); 6 XmlTextReader reader = new XmlTextReader(new StringReader(doc.OuterXml)); 7 reader.WhitespaceHandling = WhitespaceHandling.None; 8 reader.ReadToFollowing("Animal"); 9 reader.ReadToFollowing("Dog"); 10 if (reader.HasAttributes) 11 { 12 Console.WriteLine(reader.GetAttribute(0)); 13 Console.WriteLine(reader.GetAttribute("Favor")); 14 } 15 reader.ReadToFollowing("Cat"); 16 if (reader.HasAttributes) 17 { 18 Console.WriteLine(reader.GetAttribute(0)); 19 Console.WriteLine(reader.GetAttribute("Favor")); 20 } 21 reader.Close(); 22 }ReadByReader
6.编辑Xml文档:
1 public static void Modify() 2 { 3 XmlDocument doc = new XmlDocument(); 4 doc.Load(path); 5 XmlNode node = doc.SelectSingleNode("Animal/Dog"); 6 node.Attributes["Sound"].Value = "WoW!!!";//修改属性 7 XmlNode root = doc.SelectSingleNode("Animal"); 8 XmlElement dog = root.ChildNodes[0] as XmlElement; 9 dog.SetAttribute("Sound", "Wang!!");//添加或修改属性 10 dog.RemoveAttribute("Favor");//移除属性 11 XmlNode cat = root.ChildNodes[1]; 12 XmlElement newNode = doc.CreateElement("Tigger"); 13 newNode.SetAttribute("Sound", "Houhou!"); 14 root.ReplaceChild(newNode, cat);//替换节点 15 doc.Save(path); 16 }Modify
标签:XML,常用,Console,C#,tw,XmlDocument,doc,reader,new From: https://www.cnblogs.com/cfsl/p/17046652.html