C#中XML基础用法

作者:张玉昊 时间:2023-08-05 17:12:34 

什么是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);
           }

}  
   }

来源:https://www.cnblogs.com/zhangyuhao/p/10578943.html

标签:C#,XML,基础
0
投稿

猜你喜欢

  • Android studio实现简易计算器App功能

    2023-03-07 07:57:57
  • Java synchronized偏向锁的核心原理详解

    2022-12-26 12:11:58
  • C#游戏开发之实现华容道游戏

    2023-01-03 03:17:20
  • timespan使用方法详解

    2022-01-19 07:04:50
  • C#控制台下测试多线程的方法

    2022-02-19 15:43:51
  • 使用controller传boolean形式值

    2023-11-28 23:05:33
  • java开源区块链jdchain入门

    2022-08-07 11:44:44
  • Android创建服务之started service详细介绍

    2022-12-09 15:10:29
  • C#实现上位机与欧姆龙PLC通讯(FINS)

    2022-10-05 03:10:08
  • Android基于AlarmManager实现用户在线心跳功能示例

    2021-12-02 20:26:46
  • SpringBoot中的响应式web应用详解

    2022-05-01 05:47:49
  • 深入讲解SpringBoot Actuator是什么

    2022-08-21 13:44:09
  • Java 中的 xx ≠ null 是什么新语法

    2022-09-10 20:51:13
  • C#实现温度转换功能

    2021-10-06 22:54:18
  • springboot html调用js无效400问题及解决

    2023-06-24 02:11:54
  • C# WinForm制作异形窗体与控件的方法

    2023-11-07 11:09:28
  • Java NIO中四大核心组件的使用详解

    2023-10-19 17:05:13
  • 详解Java代码常见优化方案

    2023-11-29 03:13:04
  • java jpa如何自定义sql语句

    2022-08-04 14:36:52
  • Java基于Socket实现HTTP下载客户端

    2022-07-06 01:02:30
  • asp之家 软件编程 m.aspxhome.com