versucht, habe ich XML die, die wie folgt aussieht:SystemInvalidOperationException wenn deserialisiert XML in C#
<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>
einen Link zu dem, was die ganze Datei aussieht ist here
Wenn ich Visual Studio verwenden einfügen als XML-Klassen erhalte ich die folgenden Klassen:
/// <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;
}
}
}
Wenn ich versuche, sie deserialise ein XmlSerializer
wie folgt aus: (ConstituencyResults) reader.Deserialize(file);
Ich bin eine Ausnahme immer
SystemInvalidOperationException
Innerexception sagen: { "nicht zu erwarten war."}
Kann jemand helfen?
Wieder habe ich es getestet und kein Problem. Verwenden Sie den richtigen Fall 'constituenceResults' und nicht' ConstituenceResults', wie Sie es tun. Benennen Sie die generierten Klassen um. – CodeNotFound
@CodeNotFound, dass es gelöst hat, danke. Kannst du eine Antwort schreiben, die ich akzeptieren kann? – NRKirby
Ich habe es nur als Antwort hinzugefügt. – CodeNotFound