Linq to XML詳細操作

2021-08-20 22:01:46 字數 2646 閱讀 5559

xelement root = new xelement("root",

new object

),new xelement("stu",

new object

),new xelement("stu",

new object

),new xelement("stu",

new object

),new xelement("stu",

new object

),new xelement("stu",

new object)}

);console.writeline(root);

console.writeline("linq查詢");

var query = from val in root.elements() where val.attribute("num").value == "093431237" || val.attribute("name").value == "peter" select val;

console.writeline(query.count());

foreach (var item in query)

var query2 = from val in root.elements("root") select val;

console.writeline("query2查詢結果:" + query2.count());

var query3 = from val in root.elements("stu") select val;

console.writeline("query3查詢結果:" + query3.count());

foreach (var item in query3)

console.writeline("查詢結果query4");

var query4 = from val in root.elements("stu") select new ;

foreach (var item in query4)

var query5 = from val in root.elements("stu") where int.parse(val.attribute("age").value) == 18 select val;

foreach (var item in query5)

console.writeline("查詢結果query6:");

var query6 = from val in root.elements("stu")

from val2 in val.elements()

where val2.name.localname == "math"

select new ;

foreach (var item in query6)

console.writeline("查詢結果query7:");

var query7 = from val in root.elements("stu") where int.parse(val.element("math").value) < 90 select val;

foreach (var item in query7)

console.writeline("查詢結果query8排序操作:");

var query8 = from val in root.elements("stu") orderby val.attribute("age").value descending select val;

foreach (var item in query8)

console.writeline("查詢結果query9按數學成績降序操作:");

var query9 = from val in root.elements("stu") orderby val.element("math").value descending select new ;

foreach (var item in query9)

console.writeline("查詢結果query10,增加where條件的排序操作");

var query10 = from val in root.elements("stu") where int.parse(val.attribute("age").value) == 18 orderby val.element("math").value descending select new ;

foreach (var item in query10)

console.writeline("查詢結果query11");

var query11 = from val in root.elements("stu") where val.elementsafterself("stu").count() > 4 select val;

foreach (var item in query11)

console.writeline("查詢結構query12");

var query12 = from val in root.elements("stu") where val.elementsbeforeself("stu").count() > 4 select val;

foreach (var item in query12)

使用Linq to XML操作XML的屬性

using system using system.xml.linq using system.linq namespace class program console.writeline console.writeline 使用linq遍歷某個元素的屬性 var query xel.element...

LINQ to XML 學習總結

一 命名空間 system.xml.linq.dll程式集定義了三個命名空間 system.xml.linq,system.xml.schema 和 system.xml.xpath 最核心的是system.xml.linq,定義了對應 xml 文件個方面的很多態別 system.xml.linq成...

使用Linq to XML遍歷XML

using system using system.xml.linq using system.linq namespace class program console.writeline console.writeline 使用xelement遍歷 foreach xelement e inxel...