NRKirby NRKirby - 3 months ago 13
C# Question

SystemInvalidOperationException when trying to deserialize XML to C#

I have XML the that looks like this:

<constituencyResults>
<constituencyResult seqNo="1">
<consituencyId>2</consituencyId>
<constituencyName>Aberconwy</constituencyName>
<results>
<result>
<partyCode>LAB</partyCode>
<votes>8994</votes>
<share>33.00</share>
</result>
<result>
<partyCode>CON</partyCode>
<votes>7924</votes>
<share>29.10</share>
</result>
</results>
</constituencyResult>
</constituencyResults>


A link to the what the whole file looks like is here

When I use Visual Studio's Paste Special As XML Classes I get the following classes:

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ConstituencyResults
{

private constituencyResultsConstituencyResult constituencyResultField;

/// <remarks/>
public constituencyResultsConstituencyResult constituencyResult
{
get
{
return this.constituencyResultField;
}
set
{
this.constituencyResultField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class constituencyResultsConstituencyResult
{

private byte consituencyIdField;

private string constituencyNameField;

private constituencyResultsConstituencyResultResult[] resultsField;

private byte seqNoField;

/// <remarks/>
public byte consituencyId
{
get
{
return this.consituencyIdField;
}
set
{
this.consituencyIdField = value;
}
}

/// <remarks/>
public string constituencyName
{
get
{
return this.constituencyNameField;
}
set
{
this.constituencyNameField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("result", IsNullable = false)]
public constituencyResultsConstituencyResultResult[] results
{
get
{
return this.resultsField;
}
set
{
this.resultsField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte seqNo
{
get
{
return this.seqNoField;
}
set
{
this.seqNoField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class constituencyResultsConstituencyResultResult
{

private string partyCodeField;

private ushort votesField;

private decimal shareField;

/// <remarks/>
public string partyCode
{
get
{
return this.partyCodeField;
}
set
{
this.partyCodeField = value;
}
}

/// <remarks/>
public ushort votes
{
get
{
return this.votesField;
}
set
{
this.votesField = value;
}
}

/// <remarks/>
public decimal share
{
get
{
return this.shareField;
}
set
{
this.shareField = value;
}
}
}


When I try to deserialise them using an
XmlSerializer
like this:
(ConstituencyResults) reader.Deserialize(file);


I'm getting an exception saying


SystemInvalidOperationException
InnerException: {" was not expected."}


Can anyone help?

Answer

You have this issue because you renamed your classes after the generation.

If you have used "Paste XML as classes" feature, then all generated classes should have their name respecting the same letter case as it was in the XML file.

Because you renamed the constituencyResults class after the generation to ConstituencyResults you get that error you have in your question. Notice the difference in c letter.

You have two solutions:

  • undo your changes by renaming ConstituencyResults to what the genration did before your changes constituencyResults.
  • add an XmlElement attribute to all modified classes, properties etc by decorating them with XmlElement e.g. XmlElement("constituencyResults") for ConstituencyResults.