一、XML 類
1、創(chuàng)建、保存、加載和顯示 XML 文檔
static void Main(string[] args)
{
XDocument employees1 =
new XDocument(//創(chuàng)建 XML 文檔
new XElement("Employees",//創(chuàng)建根元素
new XElement("Name", "Box Smith"),//創(chuàng)建元素
new XElement("Name", "Sally Jones")//創(chuàng)建元素
)
);
employees1.Save("EmployeesFile.xml");//保存到文件
//將保存的文檔加載到新變量中
XDocument employees2 = XDocument.Load("EmployeesFile.xml");
Console.WriteLine(employees2);//顯示文檔
Console.ReadKey();
}
輸出結(jié)果:
><Employees>
<Name>Box Smith</Name>
<Name>Sally Jones</Name>
</Employees>
1)創(chuàng)建 XML 樹
static void Main(string[] args)
{
XDocument employeeDoc =
new XDocument(//創(chuàng)建文檔
new XElement("Employees",//創(chuàng)建根元素
new XElement("Employee",//第一個(gè) Employee 元素
new XElement("Name", "Box Smith"),
new XElement("PhoneNumber", "408-555-10000")),
new XElement("Employee",//第二個(gè) Employee 元素
new XElement("Name", "Sally Jones"),
new XElement("PhoneNumber", "415-555-2000"),
new XElement("PhoneNumber", "415-555-2001"))
)
);
Console.WriteLine(employeeDoc);//顯示文檔
Console.ReadKey();
}
輸出結(jié)果:
<Employees>
<Employee>
<Name>Box Smith</Name>
<PhoneNumber>408-555-10000</PhoneNumber>
</Employee>
<Employee>
<Name>Sally Jones</Name>
<PhoneNumber>415-555-2000</PhoneNumber>
<PhoneNumber>415-555-2001</PhoneNumber>
</Employee>
</Employees>
2)使用 XML 樹的值
表20-2 查詢 XML 的方法
方法名稱 | 類 | 返回類型 | 描述 |
---|---|---|---|
Nodes | Xdocument,XElement | IEnumerable<object> | 返回當(dāng)前節(jié)點(diǎn)的所有節(jié)點(diǎn)(不管是什么類型) |
Elements | Xdocument,XElement | IEnumerable<XElement> | 返回當(dāng)前節(jié)點(diǎn)的 XElement 子節(jié)點(diǎn),或所有具有某個(gè)名字的子節(jié)點(diǎn) |
Element | Xdocument,XElement | XElement | 返回當(dāng)前節(jié)點(diǎn)的第一個(gè) XElement 子節(jié)點(diǎn),或具有某個(gè)名字的子節(jié)點(diǎn) |
Descendants | XElement | IEnumerable<XElement> | 返回所有的 XElement 子代節(jié)點(diǎn),或所有具有某個(gè)名字的 XElement 子代節(jié)點(diǎn),不管它們處于當(dāng)前節(jié)點(diǎn)下什么嵌套級別 |
DescendantsAndSelf | XElement | IEnumerable<XElement> | 和 Descendants 一樣,但是包括當(dāng)前節(jié)點(diǎn) |
Ancestors | XElement | IEnumerable<XElement> | 返回所有上級 XElement 節(jié)點(diǎn),或者所有具有某個(gè)名字的上級 XElement 節(jié)點(diǎn) |
AncestorsAndSelf | XElement | IEnumerable<XElement> | 和 Ancestors 一樣,但是包括當(dāng)前節(jié)點(diǎn) |
Parent | XElement | XElement | 返回當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) |
- Nodes
//獲取 XComment 節(jié)點(diǎn)
IEnumerable<XComment> comments = xd.Nodes().OfType<XComment>();
- Elements
//獲取名為 PhoneNumer 的子 XElement 節(jié)點(diǎn)
IEnumerable<XElement> empPhones = emp.Element("PhoneNumer");
- Element
獲取第一個(gè)子 XElement 節(jié)點(diǎn)。
Element 和 Elements 方法示例:
static void Main(string[] args)
{
XDocument employeeDoc =
new XDocument(
new XElement("Employees",
new XElement("Employee",
new XElement("Name", "Box Smith"),
new XElement("PhoneNumber", "408-555-10000")),
new XElement("Employee",
new XElement("Name", "Sally Jones"),
new XElement("PhoneNumber", "415-555-2000"),
new XElement("PhoneNumber", "415-555-2001"))
)
);
XElement root = employeeDoc.Element("Employees");
IEnumerable<XElement> employees = root.Elements();
foreach(XElement emp in employees)
{
XElement empNameNode = emp.Element("Name");
Console.WriteLine(empNameNode.Value);
IEnumerable<XElement> empPhones = emp.Elements("PhoneNumber");
foreach (XElement phone in empPhones)
Console.WriteLine($" { phone.Value }");
}
Console.ReadKey();
}
輸出結(jié)果:
Box Smith
408-555-10000
Sally Jones
415-555-2000
415-555-2001
3)增加節(jié)點(diǎn)以及操作 XML
static void Main(string[] args)
{
XDocument xd = new XDocument(//創(chuàng)建 XML 樹
new XElement("root",
new XElement("first")
)
);
Console.WriteLine("Original tree");
Console.WriteLine(xd);Console.WriteLine();//顯示樹
XElement rt = xd.Element("root");//獲取第一個(gè)元素
rt.Add(new XElement("second"));//添加子元素
rt.Add(new XElement("third"),//再添加3個(gè)子元素
new XComment("Important Comment"),
new XElement("fourth"));
Console.WriteLine("Modified tree");
Console.WriteLine(xd);//顯示 Modified tree
Console.ReadKey();
}
輸出結(jié)果:
Original tree
<root>
<first />
</root>
Modified tree
<root>
<first />
<second />
<third />
<!--Important Comment-->
<fourth />
</root>
表20-3 操作 XML 的方法
方法名稱 | 從哪里調(diào)用 | 描述 |
---|---|---|
Add | 父節(jié)點(diǎn) | 在當(dāng)前節(jié)點(diǎn)的既有子節(jié)點(diǎn)前后加新的子節(jié)點(diǎn) |
AddFirst | 父節(jié)點(diǎn) | 在當(dāng)前節(jié)點(diǎn)的既有子節(jié)點(diǎn)前增加新的子節(jié)點(diǎn) |
AddBeforeSelf | 節(jié)點(diǎn) | 在同級別的當(dāng)前節(jié)點(diǎn)之前增加新的節(jié)點(diǎn) |
AddAfterSelf | 節(jié)點(diǎn) | 在同級別的當(dāng)前節(jié)點(diǎn)之后增加新的節(jié)點(diǎn) |
Remove | 節(jié)點(diǎn) | 刪除當(dāng)前所選的節(jié)點(diǎn)及其內(nèi)容 |
RemoveNodes | 節(jié)點(diǎn) | 刪除當(dāng)前所選的 XElement 及其內(nèi)容 |
SetElement | 父節(jié)點(diǎn) | 設(shè)置節(jié)點(diǎn)的內(nèi)容 |
2、使用 XML 特性
特性提供了有關(guān) XElement 節(jié)點(diǎn)的額外信息,它放在 XML 元素的開始標(biāo)簽中。
如:
static void Main(string[] args)
{
XDocument xd = new XDocument(
new XElement("root",
new XAttribute("color", "red"),//特性構(gòu)造函數(shù)
new XAttribute("Size", "large"),//特性構(gòu)造函數(shù)
new XElement("first"),
new XElement("second")
)
);
Console.WriteLine(xd);
XElement rt = xd.Element("root");//獲取元素
XAttribute color = rt.Attribute("color");
XAttribute size = rt.Attribute("Size");//獲取特性
Console.WriteLine($"color is {color.Value}");//顯示特性值
Console.WriteLine($"size is {size.Value}");
//移除特性
//rt.Attribute("color").Remove();//移除 color 特性
//rt.SetAttributeValue("Size",nulll);//移除 size 特性
//變更特性值
//rt.SetAttributeValue("Size","medium");
//rt.SetAttributeValue("width","narrow");//如果沒有這個(gè)特性,就添加特性
Console.ReadKey();
}
輸出結(jié)果:
<root color="red" Size="large">
<first />
<second />
</root>
color is red
size is large
2、其他類型的節(jié)點(diǎn)
1)XComment
new XComment("This is a comment");
XML 文檔行為:
<!--This is a comment-->
2)XDeclaration
XML 文檔從包含 XML 使用的版本號、使用的字符編碼類型以及文檔是否依賴外部引用的一行開始。
new XDeclaration("1.0", "utf-8", "yes");
XML 文檔行:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
3)XProcessingInstruction
XML 處理指令用于提供關(guān)于 XML 文檔的使用和解釋方法的額外數(shù)據(jù)。處理指令最常用于關(guān)聯(lián) XML 文檔和樣式表。
可以使用 XProcessingInstruction 構(gòu)造函數(shù)來包含處理指令。它接受兩個(gè)字符串參數(shù):目標(biāo)和數(shù)據(jù)串。如果處理指令接受多個(gè)數(shù)據(jù)參數(shù),這些參數(shù)必須包含在 XProcessingInstruction 構(gòu)造函數(shù)的第二個(gè)字符串參數(shù)中。
new XProcessingInstruction("XML-stypesheet",
@"href=""stories"", type=""text/css""")
XML 文檔行:
<?xml-stylesheet href="stories.css" type=""text/css?>
例如:
static void Main(string[] args)
{
XDocument xd = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("This is a comment"),
new XProcessingInstruction("xml-stylesheet",
@"href=""stories.css"" type=""text/css"""),
new XElement("root",
new XElement("first"),
new XElement("second")
)
);
Console.WriteLine(xd);
Console.ReadKey();
}
輸出結(jié)果:
<!--This is a comment-->
<?xml-stylesheet href="stories.css" type="text/css"?>
<root>
<first />
<second />
</root>
二、使用 LINQ to XML 的 LINQ 查詢
1)創(chuàng)建保存 XML 文檔:
示例:
static void Main(string[] args)
{
XDocument xd = new XDocument(
new XElement("MyElements",
new XElement("first",
new XAttribute("color","red"),
new XAttribute("size","small")),
new XElement("second",
new XAttribute("color", "red"),
new XAttribute("size", "medium")),
new XElement("third",
new XAttribute("color", "blue"),
new XAttribute("size", "large"))));
Console.WriteLine(xd);//顯示 XML 樹
xd.Save("SimpleSample.xml");//保存 XML 樹
Console.ReadKey();
}
XML 文檔行:
<MyElements>
<first color="red" size="small" />
<second color="red" size="medium" />
<third color="blue" size="large" />
</MyElements>
2)加載 XML 文檔,使用 LINQ 查詢 XML:
static void Main(string[] args)
{
XDocument xd = XDocument.Load("SimpleSample.xml"); //加載文檔
XElement rt = xd.Element("MyElements"); //獲取根元素
var xyz = from e in rt.Elements() //選擇名稱包含
where e.Name.ToString().Length == 5 //5個(gè)字符的元素
select e;
foreach (XElement x in xyz)
Console.WriteLine(x.Name.ToString());//顯示所選的元素
Console.WriteLine();
foreach (XElement x in xyz)
Console.WriteLine("Name:{0}, color: {1},size: {2}",
x.Name,
x.Attribute("color").Value,
x.Attribute("size").Value);
Console.ReadKey();
}
輸出結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-509715.html
first
third
Name:first, color: red,size: small
Name:third, color: blue,size: large
3)如下代碼使用了一個(gè)簡單的查詢來獲取 XML 樹的所有頂層元素,并且為每一個(gè)元素創(chuàng)建了一個(gè)匿名類型的對象。
static void Main(string[] args)
{
XDocument xd = XDocument.Load("SimpleSample.xml"); //加載文檔
XElement rt = xd.Element("MyElements"); //獲取根元素
var xyz = from e in rt.Elements()
select new { e.Name, color = e.Attribute("color") };
foreach (var x in xyz)
Console.WriteLine(x);//默認(rèn)格式化
Console.WriteLine();
foreach (var x in xyz)
Console.WriteLine("{0,-6}, color: {1,-7}",x.Name,x.color.Value);
Console.WriteLine();
Console.ReadKey();
}
//創(chuàng)建匿名類型:new { e.Name, color = e.Attribute("color") };
輸出結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-509715.html
{ Name = first, color = color="red" }
{ Name = second, color = color="red" }
{ Name = third, color = color="blue" }
first , color: red
second, color: red
third , color: blue
到了這里,關(guān)于(十一)CSharp-LINQ-LINQToXML(4)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!