20230718_082843 xml的解析 csharp
什么是xml
XML是可扩展标记语言(Extensible Markup Language)的缩写,是一种用于标记电子文件使其具有结构性的标记语言。XML的设计宗旨是传输数据,而不是显示数据。XML标签没有被预定义,您需要自行定义标签。XML被设计为具有自我描述性,这意味着您可以使用XML文档来描述数据,而无需知道如何处理这些数据的计算机程序。
xml示例
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
关键对象
使用示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
// 获取对象
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNode root = doc.DocumentElement;
Console.WriteLine(root);
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode tempNode in nodeList)
{
Console.WriteLine(tempNode);
Console.WriteLine(tempNode.Name);
Console.WriteLine(tempNode.InnerText);
}
// 卡顿
Console.ReadLine();
}
}
}