我有一个像这样的xml文件:
<root>
<lv1 value="a" >
<lv2 value="aa" >
<lv3 name="aaa" value="1" />
<lv3 name="aab" value="2" />
<lv4 name="aac" value="4" />
</lv2>
<lv2 value="bb">
<lv3 name="bba" value="2" />
<lv3 name="bbb" value="5" />
<lv3 name="bbc" value="4" />
</lv2>
</lv1>
</root>
我想找到lv3的所有值和名称lv2.value =“bb”和lv1.value =“a”
我使用linq
var lv1s = from lv1 in XDocument.Load(yourPath)
.Descendants("lv1")
.Descendants("lv2")
.Descendants("lv3")
select new
{
value = lv1.Attribute("value").Value,
name = lv1.Attribute("name").Value
};
foreach (var lv in lv1s)
{
holiday += lv.name +":" + lv.name ;
}
如何设置它的位置???
答案 0 :(得分:2)
var result = XDocument.Load(yourPath).Descendants("lv1")
.Where(x => x.Attribute("value").Value == "a")
.Descendants("lv2")
.Where(x => x.Attribute("value").Value == "bb")
.Descendants("lv3")
.Select(x => new
{
Name = x.Attribute("name").Value,
Value = x.Attribute("value").Value
});