linq to xml 获取的节点不正确
linq to xml
xml格式如下:
<?xml version="1.0"?>
<webConfig>
<Areas key="webInfo" info="网站信息">
<rootInfo indexKey="siteName" info="网站名称" value=""></rootInfo>
<rootInfo indexKey="webSiteUrl" info="网站网址" value=""></rootInfo>
</Areas>
<Areas key="pay" info="支付信息">
<rootInfo indexKey="alipayID" info="支付宝ID" value=""></rootInfo>
</Areas>
</webConfig>
代码如下
XDocument doc = XDocument.Load(configPath);
var menus = from a in doc.Descendants("Areas")
select new AreasConfig
{
key = (string)a.Attribute("key"),
info = (string)a.Attribute("info"),
root = (from r in doc.Descendants("rootInfo")
select new rootInfo
{
indexKey = (string)r.Attribute("indexKey"),
info = (string)r.Attribute("info"),
value = (string)r.Attribute("value")
}).ToList()
};
return menus;
我只想获得 “网站信息”节点下的 rootinfo,
可是获取 确实所有节点下的 rootinfo
请问错误在哪里?
------解决方案--------------------
XDocument doc = XDocument.Load(configPath);
var menus = from a in doc.Descendants("Areas")
where a.Attribute("info")=="网站信息"
select new AreasConfig
{
key = (string)a.Attribute("key"),
info = (string)a.Attribute("info"),
root = (from r in a.Elements("rootInfo")
select new rootInfo
{
indexKey = (string)r.Attribute("indexKey"),
info = (string)r.Attribute("info"),
value = (string)r.Attribute("value")
}).ToList()
};
return menus;
代码逻辑就有问题