zoukankan      html  css  js  c++  java
  • XML(可扩展标记语言)

    一、简介:

    XML优点:容易读懂;格式标准任何语言都内置了XML分析引擎;

    XML就是一种格式化的方式来存储数据;

    .net程序中的一些配置文件app.config   、 web.config 文件都是xml文件;

    XML语法规范:标签、嵌套、属性。标签要闭合,属性值要用“”包围,标签可以相互嵌套;

    XML树,父节点、子节点、兄弟节点;

    实例:

    <?xml version="1.0" encoding="utf-8" ?>
    <Person>
      <Person id="1" >
        <Age>12</Age>
        <Name>chen</Name>
      </Person>
      <Person id="2" >
        <Age>24</Age>
        <Name>wang</Name>
      </Person>
    </Person>

    二、XML语法特点:

    1、严格区分大小写

    2、有且只能有一个根节点

    3、有开始标签必须有结束标签,除非自闭合:<Person/>

    4、属性必须使用双引号

    5、文档声明:<?xml version="1.0" encoding="utf-8" ?>

    6、注释: <!--  -->

    7、注意编码问题,文本文件实际编码要与文档声明的编码一致

    三、 XML读取:

            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("d:/temp/1.xml");
                XmlNodeList personNodeList = doc.DocumentElement.ChildNodes;
                foreach (XmlNode stuNode  in personNodeList)
                {
                    XmlElement stuEle = (XmlElement)stuNode;
                    string stuid = stuEle.GetAttribute("Id");
                    XmlNode stuNameNode = stuEle.SelectSingleNode("Name");
                    string stuName = stuNameNode.InnerText;
                    Console.WriteLine(stuid, stuName);
                }
            }

    三、 XML写入:

        class Program
        {
            static void Main(string[] args)
            {
                Person[] persons = { new Person(1, "chen", 8), new Person(2, "wang", 8) };
                XmlDocument doc = new XmlDocument();
                XmlElement ePersons = doc.CreateElement("Persons");//创建Persons根节点
                doc.AppendChild(ePersons);//把Persons添加为文档的子节点,由于文档没有子节点,所以这个节点就作为根节点;根节点只能有一个
                foreach (Person p in persons)
                {
                    XmlElement eP = doc.CreateElement("Person");//创建Persion节点
                    eP.SetAttribute("Id", p.Id.ToString());  //设置属性Id=1
                    XmlElement eName = doc.CreateElement("Name");//创建Name节点
                    eName.InnerText = p.Name.ToString();  //设置创建出来的节点的内部文本
                    XmlElement eAge = doc.CreateElement("Age");
                    eAge.InnerText = p.Age.ToString();
                    eP.AppendChild(eName);//把Name节点添加到Person节点下
                    eP.AppendChild(eAge);
    ePersons.AppendChild(eP);//把Person节点添加到Persons节点下
                }
                doc.Save("d:/2.xml");
                Console.ReadKey();
            }
        }
    
        class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public Person(int id,string name,string age)
            {
                this.Id = id;
                this.Name = name;
                this.Age = age;
            }
        }
  • 相关阅读:
    ruby 二进制转十进制 Integer("0b101") = 5
    开始菜单和我的文档的我的图片及我的音乐变成 my pictrues 正常图标了
    ruby watir 莫名其妙的错误
    Excel SaveAS是去掉提示框
    apache && jboss安装
    ruby require include的区别
    ruby控制鼠标
    This error is raised because the column 'type' is reserved for storing the class in case of inheritance
    用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
  • 原文地址:https://www.cnblogs.com/fuyouchen/p/9365965.html
Copyright © 2011-2022 走看看