请输入关键字
XML操作写入与读取
Alin|2017-12-6

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace XmlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //XML:eXtensible Markup Language 可扩展标记语言


            #region 通过编程方法实现XML写入
            ////1,在内存中构建一个Dom对象
            //XmlDocument xmlDoc = new XmlDocument();
            ////2增加一个文档说明
            //XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0","utf-8","yes");
            //xmlDoc.AppendChild(xmlDec);
            ////3为一个文档增加一个根元素
            //XmlElement xmlRootElement = xmlDoc.CreateElement("school");
            //xmlDoc.AppendChild(xmlRootElement);

            ////4,为根元素里面增加子元素,接下来增加元素都要将子元素增加到xmlRootElement元素下
            //XmlElement xmlClassElement = xmlDoc.CreateElement("class");
            ////4.1 为子元素增加属性信息
            //XmlAttribute xmlAttr = xmlDoc.CreateAttribute("id");
            //xmlAttr.Value = "c01";
            ////将属性加入到Class节点上
            //xmlClassElement.Attributes.Append(xmlAttr);
            ////把class加到根节点上
            //xmlRootElement.AppendChild(xmlClassElement);


            ////5为class 元素下面再创建一个student节点
            //XmlElement xmlStudentElement = xmlDoc.CreateElement("strudent");
            //XmlAttribute xmlStudentAttr = xmlDoc.CreateAttribute("sid");
            //xmlStudentAttr.Value = "s001";
            //xmlStudentElement.Attributes.Append(xmlStudentAttr);
            //xmlClassElement.AppendChild(xmlStudentElement);

            ////6为student增加节点
            //XmlElement xmlNameElement = xmlDoc.CreateElement("name");
            //xmlNameElement.InnerText = "alin";
            //xmlStudentElement.AppendChild(xmlNameElement);
            //XmlElement xmlAgeElement = xmlDoc.CreateElement("age");
            //xmlAgeElement.InnerText = "20";
            //xmlStudentElement.AppendChild(xmlAgeElement);

            ////7保存文件 此文件如果没有指定具体路径时,。net默认保存在Debug文件中
            //xmlDoc.Save("school.xml");

            //Console.WriteLine("ok");
            //Console.ReadKey();


            #endregion

            #region 通过List集合中的内容循环写入xml中,通过XmlDocument方式写入xml文件 using System.Xml;

            //List<Person> listPerson = new List<Person>();
            //listPerson.Add(new Person() {Name="alin" ,Age=18 });
            //listPerson.Add(new Person() { Name = "ddd", Age = 48 });
            //listPerson.Add(new Person() { Name = "alsssin", Age = 88 });
            //listPerson.Add(new Person() { Name = "alifdn", Age =98 });

            ////1,创建一个Dom对象
            //XmlDocument xDoc = new XmlDocument();
            ////2,定义一个文档声明
            //XmlDeclaration xmlDec = xDoc.CreateXmlDeclaration("1.0","utf-8",null);
            //xDoc.AppendChild(xmlDec);
            //XmlElement xRoot = xDoc.CreateElement("Persons");
            //xDoc.AppendChild(xRoot);

            //for (int i = 0,length=listPerson.Count; i < length; i++)
            //{
            //    XmlElement xPerson = xDoc.CreateElement("Person");
            //    XmlAttribute xPersonAttr = xDoc.CreateAttribute("id");
            //    xPersonAttr.Value = "p00"+(i+1).ToString();
            //    xPerson.Attributes.Append(xPersonAttr);

            //    XmlElement xName = xDoc.CreateElement("Name");
            //    xName.InnerText = listPerson[i].Name;
            //    xPerson.AppendChild(xName);

            //    XmlElement xAge = xDoc.CreateElement("Age");
            //    xAge.InnerText = listPerson[i].Age.ToString();
            //    xPerson.AppendChild(xAge);

            //    xRoot.AppendChild(xPerson);


            //}

            //xDoc.Save("persons.xml");
            //Console.WriteLine("ok");
            //Console.ReadKey();

            #endregion

            #region 通过XDocument方式写入xml文件 using System.Xml.Linq;
            List<Person> listPerson = new List<Person>();
            listPerson.Add(new Person() { Name = "alin", Age = 18 });
            listPerson.Add(new Person() { Name = "ddd", Age = 48 });
            listPerson.Add(new Person() { Name = "alsssin", Age = 88 });
            listPerson.Add(new Person() { Name = "alifdn", Age = 98 });

            //1创建一个Domc对象
            XDocument xDoc = new XDocument();
            //2声明文档及编码
            xDoc.Declaration=new XDeclaration("1.0", "uft-8", null);
            
            //3创建根节点
            XElement xRootNode = new XElement("Persons");
            xDoc.Add(xRootNode);

            //4循环List集合创建Person节点
            for (int i=0,len=listPerson.Count;i<len;i++)
            {
                XElement xPersonNode = new XElement("Person");
                xPersonNode.SetAttributeValue("id", "p" + (i + 1).ToString());

                xPersonNode.SetElementValue("Name",listPerson[i].Name);
                xPersonNode.SetElementValue("Age", listPerson[i].Age);
                xRootNode.Add(xPersonNode);
            }
            //5保存到文件
            xDoc.Save("XDocumentPersons.xml");


            Console.WriteLine("写入成功");
            Console.ReadKey();
            #endregion

        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

 

//读取XML

1.遍历所有节点元素,分别读取

2,根据节点名称,或者ID等一些元素快速获取某个节点

赞一下50||已浏览2557

本站版本归木之林解释所有 copyright(C)2010-2027www.mzlin.net 备案/许可证编号为:粤ICP备15050036号