问题是XmlType(“Erand”)名称与我的Erand类相同,如果我将类名更改为Eranda它的工作原理,还有其他方式可以说.net该怎么做?
我有一个班级
public class Erand
{
public long ID { get; set; }
public string AsjaNumber { get; set; }
}
和
[XmlType("Erand")]
public class ErandTsiv : Erand
{
[XmlElement("ID_KIS")]
public long idKis { get; set; }
[XmlElement("ID_ET")]
public long idEt { get; set; }
}
我想反序列化ErandTsiv
来自xml,如
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfErand xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Erand>
<ID>573838383</ID>
<ID_KIS>573838383</ID_KIS>
<ID_ET></ID_ET>
<AsjaNumber>2-08-88785</AsjaNumber>
</Erand>
</ArrayOfErand>
像
var stream = new StringReader(erandid);
var serializer = new XmlSerializer(new List<ErandTsiv>().GetType());
var erandTsivs = (IList<ErandTsiv>)serializer.Deserialize(stream);`
但收到错误
{"There was an error reflecting type 'System.Collections.Generic.List`1[Aet.test.unit.application.utility.TsivTapsustaTest.ErandTsiv]'."}
答案 0 :(得分:2)
要反序列化这个确切的XML,您应该依赖List<ErandTsiv>
的自定义列表定义,而忽略XmlType
上的ErandTsiv
您的班级定义将是:
public class Erand
{
public long ID { get; set; }
public string AsjaNumber { get; set; }
}
public class ErandTsiv : Erand
{
[XmlElement("ID_KIS")]
public long idKis { get; set; }
[XmlElement("ID_ET")]
public long idEt { get; set; }
}
[XmlRoot("ArrayOfErand")]
public class ErandTsivList
{
public ErandTsivList()
{
Erands = new List<ErandTsiv>();
}
[XmlElement("Erand")]
public List<ErandTsiv> Erands { get; set; }
}
反序列化将是:
var stream = new StringReader(x);
var serializer = new XmlSerializer(typeof(ErandTsivList));
var erandTsivs = (ErandTsivList)serializer.Deserialize(stream);
// your List<ErandTsiv> would then be in erandTsivs.Erands