如何使用双实体名称反序列化列表?

时间:2012-04-30 19:33:53

标签: xml

我有下面列出的xml。我想反序列化到列表<>在C#中。有没有办法处理列表实体名称ASIwizard和向导的加倍?如果它只是ASIwizard或向导我可以做到但我不知道如果每个元素都有一个双重权利名称该怎么办?

<ASiwizards>
  <ASiwizard>
    <Wizard>
      <id>1</id>
      <title>Headlight Wizard</title>
      <description>This wizard will help troubleshoot issues related to the headlight functionality of the eBike controller</description>
      <created>2012-04-27 14:35:34</created>
      <modified>2012-04-27 14:35:34</modified>
    </Wizard>
  </ASiwizard>
  <ASiwizard>
    <Wizard>
      <id>2</id>
      <title>Wiring Harness</title>
      <description/>
      <created>2012-04-27 19:11:33</created>
      <modified>2012-04-27 19:11:33</modified>
      </Wizard>
  </ASiwizard>
</ASiwizards>

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

var xElement = XElement.Parse(xml);
var items = xElement
  .Elements("ASiwizard")
  .Select(x => x.Element("Wizard"))
  .Select(
    x => new {
      Id = (Int32) x.Element("id"),
      Title = (String) x.Element("title"),
      Description = (String) x.Element("description"),
      Created = (DateTime) x.Element("created"),
      Modified = (DateTime) x.Element("modified")
    }
  )
  .ToList();