C#中XML基礎(chǔ)用法
什么是XML?
XML:可擴展標記語言。
XML的作用:
純文本,兼容性強。
和HTML的區(qū)別:
xml: 主要用來處理、存儲數(shù)據(jù)。無規(guī)定標簽,可擴展。
html:對數(shù)據(jù)的顯示和描述。 語法標簽固定。
XML語法特點:
區(qū)分大小寫。
只能有一個根節(jié)點。
標簽成對出現(xiàn)。
屬性用雙引號。
沒有預(yù)定標簽,用什么寫什么
文檔聲明:<?xml version=".." encoding="...">
注釋: <!--? ?-->
CDATA: 原意文本 <![CDATA[..] ] >
xmldocument 操作:
class Program
{
static void Main(string[] args)
{
//實現(xiàn)xml的寫入
//1、在內(nèi)存中構(gòu)建Dom對象
XmlDocument xmlDoc = new XmlDocument();
//增加文檔說明
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDeclaration);
//增加根元素
// 創(chuàng)建根元素
XmlElement rootElement = xmlDoc.CreateElement("school");
xmlDoc.AppendChild(rootElement);
//3、增加子元素,接下來添加的子元素增加到rootElement節(jié)點下
XmlElement xmlClassElement = xmlDoc.CreateElement("class");
// 為class元素添加id屬性
XmlAttribute attr = xmlDoc.CreateAttribute("id");
attr.Value = "x01";
xmlClassElement.Attributes.Append(attr);
rootElement.AppendChild(xmlClassElement);
//4、為class創(chuàng)建student節(jié)點。
XmlElement xmlStudentElement = xmlDoc.CreateElement("student");
// 為student元素添加sid 屬性.
XmlAttribute studentAttr = xmlDoc.CreateAttribute("sid");
studentAttr.Value = "s011";
xmlStudentElement.Attributes.Append(studentAttr);
xmlClassElement.AppendChild(xmlStudentElement);
//student中增加name節(jié)點。
XmlElement xmlNameElement = xmlDoc.CreateElement("name");
xmlNameElement.InnerText = "天";
xmlStudentElement.AppendChild(xmlNameElement);
//2、將該Dom對象寫入xml文件中
xmlDoc.Save("school.xml");
Console.WriteLine("ok");
}
}
以上方法可以用循環(huán)寫入。
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、 創(chuàng)建Dom對象。
XDocument xDoc = new XDocument();
XDeclaration xDec = new XDeclaration("1.0", "utf-8", null);
// 設(shè)置文檔定義
xDoc.Declaration = xDec;
//2、創(chuàng)建根節(jié)點
XElement rootElement = new XElement("List");
xDoc.Add(rootElement);
//3、循環(huán)創(chuàng)建節(jié)點
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);
}
}
}
到此這篇關(guān)于C#中XML基礎(chǔ)用法的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)json格式數(shù)據(jù)解析功能的方法詳解
這篇文章主要介紹了C#實現(xiàn)json格式數(shù)據(jù)解析功能的方法,結(jié)合實例形式較為詳細的分析了C#解析json格式數(shù)據(jù)的具體操作步驟與相關(guān)注意事項,需要的朋友可以參考下2017-12-12
C#?設(shè)置Chart的X軸為時間軸???????詳情
這篇文章主要介紹了C#設(shè)置Chart的X軸為時間軸???????詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08

