0

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

<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>
4

1 回答 1

0

This should do the trick:

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();
于 2012-04-30T19:39:42.357 回答