LINQ to XML-选择XML到强类型对象 [英] LINQ to XML - selecting XML to a strongly typed object

查看:66
本文介绍了LINQ to XML-选择XML到强类型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多相关问题,但是我将把这些问题分解为单独的帖子.

I have a number of related issues but I will break the questions down into seperate posts.

我的XML是<Person>.....<Skills><Skill>Resus<Skill></Skills></Person>

我的代码是:

var products1 = from prd in xDoc.Descendants("Person") 

select new BusinessEntityLayer.Personnel
{
  PayrollNo = (String)prd.Element("PayrollNumber"),
  FirstName = (String)prd.Element("Name"),
  LastName = (String)prd.Element("Surname"),
  StreetAddress = (String)prd.Element("StreetAddress"),
  Suburb = (String)prd.Element("Suburb"),
  HomePhone = (String)prd.Element("HomePhone"),
  MobilePhone = (String)prd.Element("MobilePhone"),
  PagerNumber = (String)prd.Element("PagerNumber"),
  Email = (String)prd.Element("Email"),
  RecordType = (String)prd.Element("RecordType"),
  Skills = (List<String>)prd.Element("Skills")

我的人员类是强类型的.除技能"系列外,所有其他功能都非常完美.技能是List<Skill>,但是我的代码不会编译错误-XLInq.ElementGeneric.List ...也无法使用String[](重构我的业务类),因为得到的结果相同.

My Personnel class is strongly typed. It all works perfectly apart from the Skills collection. Skills is List<Skill> but my code won't compile with an error - XLInq.Element to Generic.List...nor can I use String[] (refactoring my business class) as I get the same result.

人们在这里使用什么策略?

What strategies do people use here?

推荐答案

我认为您应该可以执行以下操作:

I think you should be able to do something like this:

Skills = prd.Descendants("Skill").Select(e => new Skill(e.Value)).ToList(),

这篇关于LINQ to XML-选择XML到强类型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆