反序列化返回空对象
本文关键字:对象 返回 反序列化 | 更新日期: 2023-09-27 18:19:38
我想将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
中加载的文件是有效的。
编辑:我设法缩小了问题的范围。它确实正确地反序列化了列表,只有这些列表中对象的所有属性都是空的。
找到我的答案
我不得不编辑我的列表属性:
[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; }
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();
您需要从列表中重新移动[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; }
}
您想要的是XmlArray,而不是XmlElement:
[XmlArray("Artiesten")]
[XmlArrayItem("Artiest")]
public List<Artiest> ...
实际上,这是列表的默认行为,所以您也只需完全删除该属性。