我正在尝试获取多个后代并将这些值提供给类中的某些属性。我无法弄清楚的部分是如何在后代之间弄清楚:
var elements =
from descendant in doc.Descendants().Where(desc =>
desc.Name == "Name1" || desc.Name == "Name2" || desc.Name == "Name3")
select new AppInfo.Names
{
Name1 = //here I want to get the Name1 descendant .Value
Name2 = //here I want to get the Name2 descendant .Value
Name3 = //here I want to get the Name3 descendant .Value
};
有可能这样做吗?或者我是否必须通过元素进行新的迭代?
答案 0 :(得分:2)
如果AppInfo.Names只是一个名字数组,请尝试:
var filterNames = new []{"Name1", "Name2", "Name3"};
var elements = from descendant in doc.Descendants()
.Where(desc =>filterNames.Contains(desc.Name))
select descendant.Value;
AppInfo.Names = elements.ToArray();
编辑:
如果名称是属性,您可以使用类似:
的内容var names = AppInfo.Names
{
Name1 = doc.Descendants("Name1").First().Value,
Name2 = doc.Descendants("Name2").First().Value,
Name3 = doc.Descendants("Name3").First().Value
}