<?xml version="1.0" encoding="utf-8"?>
<WorkflowVml>
<BtnList id="5">
<BtnSingle id="A001" name="001" defaultUrl="xxx.aspx" ></BtnSingle>
<BtnSingle id="A002" name="002" defaultUrl="xxx.aspx" ></BtnSingle>
<BtnSingle id="A003" name="003" defaultUrl="xxx.aspx" ></BtnSingle>
<BtnSingle id="A004" name="004" defaultUrl="xxx.aspx" ></BtnSingle>
</BtnList>
<BtnList id="7">
<BtnSingle id="A001" name="001" defaultUrl="xxx.aspx" ></BtnSingle>
<BtnSingle id="A002" name="002" defaultUrl="xxx.aspx" ></BtnSingle>
<BtnSingle id="A003" name="003" defaultUrl="xxx.aspx" ></BtnSingle>
<BtnSingle id="A004" name="004" defaultUrl="xxx.aspx" ></BtnSingle>
</BtnList>
</WorkflowVml>
XDocument RoAuXml = XDocument.Load(Server.MapPath("Competence.xml"));
var roauList = from roau in RoAuXml.Descendants("BtnList")
where roau.FirstAttribute.Value == "7"
select roau.Elements("BtnSingle");
然后我不知道怎么从roauList里面用linq把BtnSingle的id等于A001,A002的,name,defaulturl读出来呢?
哈,好高的分。
这个问题如果要用一条简单的语句实现是很难的,而且性能上也不合算,应该分出多条语句来实现最好。下面给你个函数,抛砖引玉,希望能对你有启发。如有问题,可以继续补充。
public IEnumerable GetButtons(XDocument doc, string listID, params string[] btnIds) { var q = from e in doc.Elements("BtnList") where e.Attribute("id").Value == listID select e.Elements("ButtonSingle"); return from c in q.Single() where btnIds.Contains(c.Attribute("id").Value) select new { Name = c.Attribute("name").Value, DefaultUrl = c.Attribute("defaulturl").Value }; }
你好,如果 BtnList 的FirstAttribute 的值是唯一的话 我个人觉得可以这样
var m = from l in (from k in roauList select k).FirstOrDefault() where l.FirstAttribute.Value == "A0001" || l.FirstAttribute.Value == "A0002" select l;
或者
var m = from l in (from k in roauList select k).FirstOrDefault() where l.Attribute("id").Value == "A0001" || l.Attribute("id").Value == "A0002" select l;
不知道能否帮到你
我的BtnSingle的id等于A001,A002,(A001,A002)它是可变的,它有可能是('A001'),或者是('A001','A002','A003')我想是Where的时候id in('A001','A002')
@山古水月:
List<string> list = new List<string>() {"A001","A002" };
var m = from l in (from k in roauList select k).FirstOrDefault() where list.Contains(l.Attribute("id").Value) select l;
XDocument RoAuXml = XDocument.Load("Competence.xml");
var roaus=RoAuXml.Descendants("BtnList");
var roauList = from roau in roaus
select roau.Elements("BtnSingle").Where(m => m.FirstAttribute.Value == "A001");
XDocument RoAuXml = XDocument.Load("Competence.xml");
var roaus=RoAuXml.Descendants("BtnList");
var roauList = from roau in roaus
select roau.Elements("BtnSingle").Where(m =>(new string[]{"A001", "A002"}).Contains( m.FirstAttribute.Value));
var roaulitItems=roaulit.where(a=>a.Elements("BtnSingle").where(b=>b.id=="A001"||b.id== "A002").select(new {Name=b.Attribute["name"].Value,DefaultUrl=b.Attribute["defaulturl"].value}));