我試圖反序列化下面的XML:反序列化XML返回null集合屬性
<?xml version="1.0" encoding="utf-8" ?>
<mf:somedata xmlns:mf="urn:somedata">
<CurrentAccount>
<AccountType>test</AccountType>
<Charge>
<ChargeType>test</ChargeType>
</Charge>
</CurrentAccount>
<CurrentAccount>
<AccountType>test 2</AccountType>
<Charge>
<ChargeType>test 2</ChargeType>
</Charge>
</CurrentAccount>
</mf:somedata>
使用以下類:
[XmlRoot("somedata", Namespace = "urn:somedata")]
public class MfCurrentAccounts
{
[XmlElement("CurrentAccount")]
public CurrentAccount[] CurrentAccounts { get; set; }
}
public class CurrentAccount
{
public string AccountType { get; set; }
[XmlElement("Charge")]
public Charge[] Charges { get; set; }
}
public class Charge
{
public string ChargeType { get; set; }
}
var c = new XmlSerializer(typeof(MfCurrentAccounts)).Deserialize(new StringReader(xml)) as MfCurrentAccounts;
c.CurrentAccounts // <-- is always null
但無論我怎麼努力,該CurrentAccounts陣列null當我反序列化它。我試過每個組合都可以考慮的屬性(我也試過XmlArray和XmlArrayItem)。
我在做什麼錯? :S
對不起,用反序列化代碼更新了問題。 – Tom 2013-02-08 19:38:07
你不需要用'[Serializable()]'標記類嗎? – Nope 2013-02-08 19:38:57
@François我已經嘗試過,但顯然不需要。 – Tom 2013-02-08 19:39:31