• C# XML入门


    什么是XML?

    XML:可扩展标记语言。

    XML的作用:

    纯文本,兼容性强。

    和HTML的区别:

    xml: 主要用来处理、存储数据。无规定标签,可扩展。

    html:对数据的显示和描述。 语法标签固定。

    XML语法特点:

    区分大小写。

    只能有一个根节点。

    标签成对出现。

    属性用双引号。

    没有预定标签,用什么写什么

    文档声明:<?xml version=".." encoding="...">

    注释: <!--   -->

    CDATA: 原意文本 <![CDATA[..] ] >

    xmldocument 操作:

     class Program
        {
            static void Main(string[] args)
            {
                //实现xml的写入
                //1、在内存中构建Dom对象
                XmlDocument xmlDoc = new XmlDocument();
                //增加文档说明
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
                xmlDoc.AppendChild(xmlDeclaration);
                //增加根元素
                //  创建根元素
                XmlElement rootElement = xmlDoc.CreateElement("school");
                xmlDoc.AppendChild(rootElement);
                //3、增加子元素,接下来添加的子元素增加到rootElement节点下
                XmlElement xmlClassElement = xmlDoc.CreateElement("class");
                // 为class元素添加id属性
                XmlAttribute attr = xmlDoc.CreateAttribute("id");
                attr.Value = "x01";
                xmlClassElement.Attributes.Append(attr);
                rootElement.AppendChild(xmlClassElement);
                //4、为class创建student节点。
                XmlElement xmlStudentElement = xmlDoc.CreateElement("student");
                // 为student元素添加sid 属性.
                XmlAttribute studentAttr = xmlDoc.CreateAttribute("sid");
                studentAttr.Value = "s011";
                xmlStudentElement.Attributes.Append(studentAttr);
                xmlClassElement.AppendChild(xmlStudentElement);
                //student中增加name节点。
                XmlElement xmlNameElement = xmlDoc.CreateElement("name");
                xmlNameElement.InnerText = "";
                xmlStudentElement.AppendChild(xmlNameElement);
    
    
                //2、将该Dom对象写入xml文件中
                xmlDoc.Save("school.xml");
                Console.WriteLine("ok");
            }
        }

    以上方法可以用循环写入。

    xdocument 操作。

     class Program
        {
            static void Main(string[] args)
            {
                //  通过xdocument 写入文件
                List<Person> list = new List<Person>();
                list.Add(new Person() { Name = "Sam", Age = 18 });
                list.Add(new Person() { Name = "Penny", Age = 20 });
                // 1、 创建Dom对象。
                XDocument xDoc = new XDocument();
                XDeclaration xDec = new XDeclaration("1.0", "utf-8", null);
                // 设置文档定义
                xDoc.Declaration = xDec;
                //2、创建根节点
                XElement rootElement = new XElement("List");
                xDoc.Add(rootElement);
                //3、循环创建节点
                for (int i = 0; i < list.Count; i++)
                {
                    XElement PersonElement = new XElement("Person");
                    PersonElement.SetAttributeValue("id", (i + 1).ToString());
    
                    PersonElement.SetElementValue("Name", list[i].Name);
                    PersonElement.SetElementValue("Age", list[i].Age);
                    rootElement.Add(PersonElement);
                }
                xDoc.Save("List.xml");
                Console.WriteLine("ok");
            }
        }
        class Person
        {
            public string Name { get; set; }
            public  int Age { get; set; }
        }  
     class Program
        {
            static void Main(string[] args)
            {
                //读取XML文件。
                XDocument document = XDocument.Load("List.xml");
                XElement rootElement = document.Root;
                Console.WriteLine("订购人:{0}",rootElement.Element("CustomerName").Value);
                foreach (var item in rootElement.Element("Items").Elements("OrderItem"))
                {
                    Console.WriteLine("商品名称:{0}",item.Attribute("Name").Value);
                }
                
            }  
        }
  • 相关阅读:
    9-10【H5混合实战】基于自定义WebView实现H5混合开发-3
    9-9【H5混合实战】基于定义WebView实现H5混合开发-2
    9-7【实战引用】球区入口实现
    9-5&9-6创建组件,使用组件
    9-4DAO首页大接口Dao层实现和调用-2
    9-4DAO首页大接口Dao层实现和调用-1
    9.3model层设计
    9.2熟悉接口好开发
    8-9 Flutter与Native通信-Android端实战
    8-8 Flutter与Native通信-Android端讲解
  • 原文地址:https://www.cnblogs.com/zhangyuhao/p/10578943.html
Copyright © 2020-2023  润新知