2
這是反序列化的代碼:反序列化XML返回空
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>
沒有考慮'linq2xml'的任何具體原因 – Anirudha
如果有疑問,請在內存中創建一個帶有myItem的樣本myList,並將其序列化爲XML,然後將其與您嘗試讀取的源進行比較。通常這樣你就可以看到有什麼不同,需要改變什麼才能使之發揮作用。編輯:例如,我敢打賭,你有資格佩戴的方式,可能會出來爲' ... '(雖然我總是要仔細檢查) –
你有控制權數據源(可以更改XML模式)還是已修復? –