Linq 多元素选择
我认为我对 LINQ 有很好的相对性理解,但是,我在从 XML 文件中选择多个元素时遇到了困难。以下是 XML 数据的示例。
<Person>
<Addresses>
<Address>1021 E. Ray Rd</Address>
<Address>453 E. John Rd</Address>
</Addresses>
<Picture>www.testpic.com/pic/2834723</Picture>
</Person>
因此,我尝试解析这些数据并使用结果创建我的视图模型人员对象。这是我的视图模型的示例。
Public class personmodel
{
public string picURI {get;set;}
public List<string> addressLst {get;set;}
}
关于如何使用多个地址元素填充我的 addressLst 有什么见解吗?这就是我想要做的。
var xmlPeople = XElement.Parse(document)
var person = from personfile in xmlPeople.Descendants("Person")
select new personmodel
{
picURI = (string)personfile.Element("Picture"),
addressLst = personfile.Descendants("Addresses").Select(item =>item.Element("Address").Value.ToList(),
};
这可以获取地址元素下的第一个元素并将其添加到艺术家列表中。如何扩展它以将所有地址元素添加到列表中?任何帮助将不胜感激!谢谢你!
I think I have a relativity good understanding of LINQ however, I am stumbling on selecting multiple elements from an XML file. Here is a sample of the XML data.
<Person>
<Addresses>
<Address>1021 E. Ray Rd</Address>
<Address>453 E. John Rd</Address>
</Addresses>
<Picture>www.testpic.com/pic/2834723</Picture>
</Person>
So I am trying to parse this data and create my viewmodel person object with the result. Here is a sample of my view model.
Public class personmodel
{
public string picURI {get;set;}
public List<string> addressLst {get;set;}
}
Any insight on how I can fill my addressLst with the multiple address elements? Here is what I was trying to do..
var xmlPeople = XElement.Parse(document)
var person = from personfile in xmlPeople.Descendants("Person")
select new personmodel
{
picURI = (string)personfile.Element("Picture"),
addressLst = personfile.Descendants("Addresses").Select(item =>item.Element("Address").Value.ToList(),
};
This works to grab the first element under the Addresses element and add it to the artists list. How can I extend this to add all the address elements to the list? Any help would be greatly appreciated! Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)