软件编程
位置:首页>> 软件编程>> C#编程>> C#中XML基础用法

C#中XML基础用法

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

标签: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);
           }

}  
   }

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com