我有以下的文件,我需要序列化對象:連載XML文件到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>
注:完整的文件可以發現here
如何代表這XML作爲C#對象?
到目前爲止,我已經試過
- 粘貼特殊作爲XML類
- SimpleXMLToCode
但是,無論給我正確的POCO實體我需要....
我從Paste Special As XML類獲取以下類:
/// <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;
}
}
}
當我使用XmlSerializer
做(ConstituencyResults) reader.Deserialize(file);
我得到:
你想在運行時該做或編譯時間? –
這個文件來自哪裏 - 你有控制權嗎?爲什麼不使用標準的.NET Xml Serializer? – EJoshuaS
向我們展示「特殊粘貼爲XML類」的結果 – CodeNotFound