<?xml version="1.0" encoding="utf-8" ?>
<root>
<room>
<id>1</id>
<people>
<name>张三1</name>
</people>
<people>
<name>张三2</name>
</people>
</room>
<room>
<id>2</id>
<people>
<name>李四1</name>
</people>
<people>
<name>李四2</name>
</people>
</room>
</root>
类似的sql语句:select all [people] from XmlFile where room.id=1
就是说查询room的id=1下面的所有people元素。
最好采用下面的语法:
xDocument.Descendants("people").Select(
p=>new People//类
{
Name = p.Element("name").Value
}).Where(p=>p.Parent.id = 1);
//如果从子级判断父级不行,可以从父级找子级。
xDocument.Descendants("room").Select(
//在这里遍历子级
).Where(id=1);
我对Linq To Xml太陌生,大概表达一下意思,希望高手能看懂。
有时间彻底学一下,先偷懒请高手解答这个问题。
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?><root><room><id>1</id><people><name>张三1</name></people><people><name>张三2</name></people></room><room><id>2</id><people><name>李四1</name></people><people><name>李四2</name></people></room></root>";
var obj = XDocument.Parse(xml).Descendants("room").Where(w => w.Elements("id").Any(a => (int)a == 1)).SelectMany(s => s.Elements("people"));
foreach (var info in obj) Msg.Write(info + "<br />");