开发者

LINQ to XML - selecting XML to a strongly typed object

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

My XML is <Person>.开发者_高级运维....<Skills><Skill>Resus<Skill></Skills></Person>

My code is :

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")

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(),
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜