網站首頁 編程語言 正文
什么是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
相關推薦
- 2022-06-23 Python+Turtle制作獨特的表白圖_python
- 2023-03-27 Android進階之從IO到NIO的模型機制演進_Android
- 2022-10-10 Python3?re.search()方法的具體使用_python
- 2022-10-17 在?C#?中使用?Span<T>?和?Memory<T>?編寫高性能代碼的詳
- 2023-01-08 Android的VSYNC機制和UI刷新流程示例詳解_Android
- 2022-10-23 Python?Pandas數據合并pd.merge用法詳解_python
- 2022-10-29 SQL?Server主鍵約束(PRIMARY?KEY)_MsSql
- 2022-07-01 Python基于文件內容實現查找文件功能_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支