Reputation: 31
I have a fixed, valid XML document. I'm trying to deserialize it in order to get an object hierarchy. However there's an exception being thrown.
This is my XML document:
<ROWSET>
<ROW>
<LOT>LOT1234</LOT>
<DATE_TRANS>2012-05-20</DATE_TRANS>
<NUMERO_AA227>AA227_001</NUMERO_AA227>
<NUMERO_ETUI>ETUI_001</NUMERO_ETUI>
<IDENTITE_BOITE_1>Boite1_1</IDENTITE_BOITE_1>
<IDENTITE_BOITE_2>Boite1_2</IDENTITE_BOITE_2>
<IDENTITE_BOITE_3>Boite1_3</IDENTITE_BOITE_3>
<IDENTITE_BOITE_4>Boite1_5</IDENTITE_BOITE_4>
<IDENTITE_BOITE_5>Boite1_5</IDENTITE_BOITE_5>
</ROW>
<ROW>
<LOT>LOT5678</LOT>
<DATE_TRANS>2012-05-20</DATE_TRANS>
<NUMERO_AA227>AA227_001</NUMERO_AA227>
<NUMERO_ETUI>ETUI_001</NUMERO_ETUI>
<IDENTITE_BOITE_1>Boite1_1</IDENTITE_BOITE_1>
<IDENTITE_BOITE_2>Boite1_2</IDENTITE_BOITE_2>
<IDENTITE_BOITE_3>Boite1_3</IDENTITE_BOITE_3>
<IDENTITE_BOITE_4>Boite1_5</IDENTITE_BOITE_4>
<IDENTITE_BOITE_5>Boite1_5</IDENTITE_BOITE_5>
</ROW>
</ROWSET>
And this is my object model:
[Serializable]
[System.Xml.Serialization.XmlRoot("DTOFournitureListeImporter")]
public class DTOFournitureListeImporter
{
[XmlArray("ROWSET")]
[XmlArrayItem("ROW", typeof(DTOFournitureImporter))]
public DTOFournitureImporter[] dtoFournitureImporter { get; set; }
}
[Serializable]
public class DTOFournitureImporter
{
[System.Xml.Serialization.XmlElement("lot")]
public string lot { get; set; }
[System.Xml.Serialization.XmlElement("date_trans")]
public DateTime date_trans { get; set; }
[System.Xml.Serialization.XmlElement("numero_aa227")]
public string numero_aa227 { get; set; }
[System.Xml.Serialization.XmlElement("numero_etui")]
public string numero_etui { get; set; }
[System.Xml.Serialization.XmlElement("identite_boite_1")]
public string identite_boite_1 { get; set; }
[System.Xml.Serialization.XmlElement("identite_boite_2")]
public string identite_boite_2 { get; set; }
[System.Xml.Serialization.XmlElement("identite_boite_3")]
public string identite_boite_3 { get; set; }
[System.Xml.Serialization.XmlElement("identite_boite_4")]
public string identite_boite_4 { get; set; }
[System.Xml.Serialization.XmlElement("identite_boite_5")]
public string identite_boite_5 { get; set; }
}
How I deserialize:
XmlSerializer serializer = new XmlSerializer(typeof(DTOFournitureListeImporter));
TextReader textReader = new StreamReader(model.cheminFichierXML);
DTOFournitureListeImporter dTOFournitureListeImporter = (DTOFournitureListeImporter)serializer.Deserialize(textReader);
textReader.Close();
And the error:
There is an error in XML document (2, 2). - System.InvalidOperationException: was not expected. Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderDTOFournitureListeImporter.Read4_DTOFournitureListeImporter()
Upvotes: 3
Views: 667
Reputation: 56727
The problem could be that all the tags in the XML file are in capital letters, but the attributes in your class suggest they should be in small letters.
Also, you're saying in the attributes that the XML root is DTOFournitureListeImporter
, which is not the case. The XML root is ROWSET
.
So all in all: The structure you're trying to create does not match the XML file.
Upvotes: 4