0

我想将 XML 文件反序列化为 C# 对象。我的对象如下

[Serializable]
[XmlRoot(ElementName = "Collection")]
public class Collection
{
    public Collection() 
    {
        Artiesten = new List<Artiest>();
        Albums = new List<Album>();
        Nummers = new List<Nummer>();
    }
    [XmlElement("Artiesten")]
    public List<Artiest> Artiesten { get; set; }
    [XmlElement("Albums")]
    public List<Album> Albums { get; set; }
    [XmlElement("Nummers")]
    public List<Nummer> Nummers { get; set; }

}

[Serializable]
public class Artiest
{
    [XmlAttribute("artiestid")]
    public int ArtiestId { get; set; }
    [XmlElement(ElementName = "Naam")]
    public String Naam { get; set; }
    [XmlElement(ElementName = "Albums")]
    public List<Album> Albums { get; set; } 
}

[Serializable]
public class Nummer
{
[XmlAttribute("nummerid")]
    public int NummerId { get; set; }
    [XmlElement(ElementName = "titel")]
    public String Titel { get; set; }
    [XmlElement(ElementName = "duur")]
    public String Duration { get; set; }
}

我的 XML 是这样的:

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Artiesten>
    <Artiest artiestid="1">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
    </Artiest>
  </Artiesten>
  <Albums>
    <Album albumid="1">
      <Titel>Album1</Titel>
      <prijs valuta="Euro">19.99</prijs>
      <uitgiftejaar>1999</uitgiftejaar>
      <Nummers>
        <Nummer nummerid="1">
          <titel>Happy Sundays</titel>
          <duur>PT02M02S</duur>
        </Nummer>
      </Nummers>
    </Album>
  </Albums>
  <Nummers>
    <Nummer nummerid="1">
      <titel>Happy Sundays</titel>
      <duur>PT02M02S</duur>
    </Nummer>
  </Nummers>
</Collection>

我试图像这样反序列化:

XDocument doc = XDocument.Load(file);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Collection));
                using (var reader = doc.Root.CreateReader())
                {
                    Collection collection = (Collection) xmlSerializer.Deserialize(reader);

                }

由于某种原因,我找不到Collection对象中的列表都是空的。调试显示加载的文件XDocument是有效的。

编辑:我设法缩小了问题的范围。它确实正确反序列化列表,只有这些列表中对象的所有属性都是空的。

4

4 回答 4

1

找到我的答案

我不得不将我的列表属性编辑为此:

    [XmlElement("Artiesten", typeof(List<Artiest>))]
    public List<Artiest> Artiesten { get; set; }
    [XmlElement("Albums", typeof(List<Album>))]
    public List<Album> Albums { get; set; }
    [XmlElement("Nummers", typeof(List<Nummer>))]
    public List<Nummer> Nummers { get; set; }
于 2013-06-23T12:59:51.627 回答
0

你想要 XmlArray,而不是 XmlElement:

[XmlArray("Artiesten")]
[XmlArrayItem("Artiest")]
public List<Artiest> ...

实际上这是列表的默认行为,因此您也只需完全删除该属性。

于 2013-06-23T14:31:00.880 回答
0
Collection collection = null;
string path = "file.xml";

XmlSerializer serializer = new XmlSerializer(typeof(Collection));

StreamReader reader = new StreamReader(path);
collection = (Collection)serializer.Deserialize(reader);
reader.Close();
于 2013-06-23T12:47:44.757 回答
0

您需要[XmlElement]从列表中删除标签。否则,它适用于不同的 XML 结构。

例如,与其将所有Artiest对象嵌套在单个Artiesten元素中(这是您当前的 XML),它实际上会将它们设置为彼此相邻,如下所示:

  <Artiesten artiestid="1">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
  </Artiesten>
  <Artiesten artiestid="2">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
  </Artiesten>
  <Artiesten artiestid="3">
      <Naam>Harry</Naam>
      <Albums>
        <Album albumid="1">
          <Titel>Album1</Titel>
          <prijs valuta="Euro">19.99</prijs>
          <uitgiftejaar>1999</uitgiftejaar>
          <Nummers>
            <Nummer nummerid="1">
              <titel>happy Sundays</titel>
              <duur>PT02M02S</duur>
            </Nummer>
          </Nummers>
        </Album>
      </Albums>
  </Artiesten>

所以尝试重新定义你的类:

[Serializable]
[XmlRoot(ElementName = "Collection")]
public class Collection
{
    public Collection() 
    {
        Artiesten = new List<Artiest>();
        Albums = new List<Album>();
        Nummers = new List<Nummer>();
    }

    public List<Artiest> Artiesten { get; set; }
    public List<Album> Albums { get; set; }
    public List<Nummer> Nummers { get; set; }

}

[Serializable]
public class Artiest
{
    [XmlAttribute("artiestid")]
    public int ArtiestId { get; set; }
    [XmlElement(ElementName = "Naam")]
    public String Naam { get; set; }

    public List<Album> Albums { get; set; } 
}
于 2013-06-23T12:59:57.567 回答