Sure - so you need something like this:
var query = from datas in doc.Root.Elements("myDatas")
let code = (string) datas.Attribute("code")
from data in datas.Elements("myData")
select new MyData {
MainCode = code,
Code = (string) data.Attribute("name"),
Value = (string) data.Attribute("value"),
};
var list = query.ToList();
Note the multiple from
clauses to flatten the results.
Another alternative would have been to just find all the "leaf" elements and fetch the code part from the parent:
var query = from data in doc.Descendants("myData")
select new MyData {
MainCode = (string) data.Parent.Attribute("code"),
Code = (string) data.Attribute("name"),
Value = (string) data.Attribute("value"),
};
var list = query.ToList();
EDIT: If your document uses namespaces, that's easy too:
XNamespace ns = "http://the-uri-of-the-namespace";
var query = from data in doc.Descendants(ns + "myData")
...
This uses the XName operator +(XNamespace, string)
overloaded operator.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…