这是反序列化的代码:
XmlRootAttribute xRoot = new System.Xml.Serialization.XmlRootAttribute();
xRoot.ElementName = "myList";
xRoot.IsNullable = true;
xRoot.Namespace = "http://schemas.datacontract.org/2006/05/Blah.Blah.Blah";
XmlSerializer serializer = new XmlSerializer(typeof(myList), xRoot);
XmlReader reader = new System.Xml.XmlTextReader(url);
myList myDeserializedList = (myList)serializer.Deserialize(reader);
reader.Close();
而且 myDeserializedList 是空的,尽管当我转到 URL 时,我看到了一个相当大的 XML。
这是我的课程:
[Serializable()]
public class myItem
{
[System.Xml.Serialization.XmlElement("Key")]
public long Key { get; set; }
[System.Xml.Serialization.XmlElement("Discount")]
public double Discount { get; set; }
}
[Serializable, System.Xml.Serialization.XmlRoot("myList")]
public class myList
{
[System.Xml.Serialization.XmlArray("myList")]
[System.Xml.Serialization.XmlArrayItem("myItem", typeof(myItem))]
public List<myItem> myItem { get; set; }
}
这是xml:
<myList xmlns="http://schemas.datacontract.org/2006/05/Blah.Blah.Blah" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<myItem>
<Key>3465</Key>
<Discount>0.00000000</Discount>
</myItem>
</myList>