資源簡介
LINQ查詢xml文件
1、查找具有特定屬性的元素
2、查找具有特定子元素的元素
代碼片段和文件信息
using?System;
using?System.Linq;
using?System.xml.Linq;
namespace?_4_12
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????XElement?root?=?XElement.Load(@“D:\A\4-12\class.xml“);
????????????Console.WriteLine(root);
????????????var?userLst?=?
????????????????from?ele?in?root.Elements(“User“)
????????????????select?ele;
????????????foreach?(var?item?in?userLst)?
????????????{
????????????????Console.WriteLine(item);
????????????}
????????????var?nameLst?=?
????????????????from?ele?in?root.Elements(“User“)
????????????????select?ele.Attribute(“Name“).Value;
????????????foreach?(string?item?in?nameLst)?
????????????{
????????????????Console.WriteLine(item);
????????????}
????????????var?ageLst?=
????????????????from?ele?in?root.Elements(“User“)
????????????????where?int.Parse(ele.Attribute(“Age“).Value)?>?20
????????????????select?ele.Attribute(“Name“).Value;
????????????foreach?(var?item?in?ageLst)?
????????????{
????????????????Console.WriteLine(item);
????????????}
???????????
????????}
????}
}
- 上一篇:畫圖軟件源碼
- 下一篇:USB HID源碼(附文檔)
評論
共有 條評論