C#操作XML-创建-追加-带属性的读写(全)

来源:互联网 发布:影视播放源码 编辑:程序博客网 时间:2024/06/05 17:30

by 刘壮 | 2016/04/25 23:14

你好,我曾经接过一个上海的上机面试题,涉及到xml。今天分享一下c#是如何操作xml的。

大纲:
1.创建XML并实现保存
2.向XML中添加属性
3.向XML中追加内容
4.读取XML文件
5.读取带属性的XML文件,含有移除(有一句提到)

1.创建XML并实现保存
首先,我们要实现添加不含属性的xml文档。这里给出一个类,和一个调用方法以及贴出效果图(案例中我用的中文xml,希望不给你带来观看效果)

public static void CreateXML(string xmlName)        {            //通过代码创建XML文档            //1、引用命名空间   System.Xml            //2、创建一个 xml 文档            XmlDocument xml = new XmlDocument();            //3、创建一行声明信息,并添加到 xml 文档顶部            XmlDeclaration decl = xml.CreateXmlDeclaration("1.0", "utf-8", null);            xml.AppendChild(decl);            //4、创建根节点            XmlElement rootEle = xml.CreateElement("人");            xml.AppendChild(rootEle);            //5、创建子结点|属性:性别            XmlElement childEle = xml.CreateElement("性别");            rootEle.AppendChild(childEle);            XmlElement c2Ele = xml.CreateElement("男");            c2Ele.InnerText = "1";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("女");            c2Ele.InnerText = "0";            childEle.AppendChild(c2Ele);            //6、创建子节点|属性:四肢            childEle = xml.CreateElement("胳膊");            rootEle.AppendChild(childEle);            c2Ele = xml.CreateElement("右胳膊");            c2Ele.InnerText = "一般";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("左胳膊");            c2Ele.InnerText = "一般";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("左退");            c2Ele.InnerText = "粗壮";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("右腿");            c2Ele.InnerText = "粗壮";            childEle.AppendChild(c2Ele);            xml.Save(xmlName);        }

调用方法:

 CreateXML("People.xml");

效果图:
这里写图片描述

2.向XML中添加属性

在上面的类中, 最后的xml.Save(xmlName);前添加下面的代码

            //添加带有属性的节点            childEle = xml.CreateElement("hair");            childEle.SetAttribute("Color", "black");            childEle.InnerText = "头发";            rootEle.AppendChild(childEle);

运行,查看你的xml文档,就会多出来一行:

  <hair Color="black" />

效果如图:
这里写图片描述

3.向XML中追加内容
创建一个控制台程序:
Main函数如下:

     static void Main(string[] args)        {            //创XML建对象            XmlDocument doc = new XmlDocument();            //声明根节点            XmlElement books;            //判断文件是否存在            if (File.Exists("Books.xml"))            {                //该文件存在                //加载文件                doc.Load("Books.xml");                //获得根节点                books = doc.DocumentElement;            }            else//该文件不存在            {                //创建声明                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);                doc.AppendChild(dec);                //创建根节点                books = doc.CreateElement("Books");                doc.AppendChild(books);            }            //开始正常写入信息就可以了            XmlElement book1 = doc.CreateElement("Book");            books.AppendChild(book1);            XmlElement name = doc.CreateElement("Name");            name.InnerText = "大话西游"+(new Random()).Next(0,1000);            book1.AppendChild(name);            doc.Save("Books.xml");            Console.WriteLine("追加完成");            Console.ReadKey();        }

运行两次,效果图:
这里写图片描述
4.读取XML文件
这里没有详细写,重点在第5条。你可以找到答案

            //获得子节点   返回数组            XmlNodeList xnl =  books.ChildNodes;            foreach (XmlNode  item in xnl)            {                Console.WriteLine(item.InnerXml+":"+ item.InnerText);            }

5.读取带属性的XML文件
创建一个控制台程序,你不需要关心xml的创建等等,我写好了,都在这里Program.cs代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace _027创建XML文档{    class Program    {        static void Main(string[] args)        {            CreateXML("People.xml");            ReadXML("People.xml");            Console.WriteLine("创建成功");            Console.ReadKey();        }        public static void ReadXML(string filename)        {            //创建对象            XmlDocument xml = new XmlDocument();            //加载 xml 文件            xml.Load(filename);            //获取根节点            XmlElement rootEle = xml.DocumentElement;            //获取子节点集合            XmlNodeList xnl = xml.SelectNodes("/人/胳膊/左胳膊");            foreach (XmlNode item in xnl)            {                Console.WriteLine(item.Attributes["毛"].InnerText);// .Value);                //Console.WriteLine(item.Attributes["Hair"].Value);            }            //5、移除            //xnl.RemoveAll();        }        public static void CreateXML(string xmlName)        {            //通过代码创建XML文档            //1、引用命名空间   System.Xml            //2、创建一个 xml 文档            XmlDocument xml = new XmlDocument();            //3、创建一行声明信息,并添加到 xml 文档顶部            XmlDeclaration decl = xml.CreateXmlDeclaration("1.0", "utf-8", null);            xml.AppendChild(decl);            //4、创建根节点            XmlElement rootEle = xml.CreateElement("人");            xml.AppendChild(rootEle);            //5、创建子结点|属性:性别            XmlElement childEle = xml.CreateElement("性别");            rootEle.AppendChild(childEle);            XmlElement c2Ele = xml.CreateElement("男");            c2Ele.InnerText = "1";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("女");            c2Ele.InnerText = "0";            childEle.AppendChild(c2Ele);            //6、创建子节点|属性:四肢            childEle = xml.CreateElement("胳膊");            rootEle.AppendChild(childEle);            c2Ele = xml.CreateElement("右胳膊");            c2Ele.InnerText = "一般";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("左胳膊");            c2Ele.SetAttribute("毛", "真密");            c2Ele.InnerText = "一般";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("左退");            c2Ele.InnerText = "粗壮";            childEle.AppendChild(c2Ele);            c2Ele = xml.CreateElement("右腿");            c2Ele.InnerText = "粗壮";            childEle.AppendChild(c2Ele);            //添加带有属性的节点            childEle = xml.CreateElement("hair");            childEle.SetAttribute("Color", "black");            childEle.InnerText = "头发";            rootEle.AppendChild(childEle);            xml.Save(xmlName);        }    }}

运行,你会得到:“真密”

结束!!!
Web前后端技术交流群:
点击链接加入群【Web前后端技术①群】:http://jq.qq.com/?_wv=1027&k=29uDHtR

1 0
原创粉丝点击