Xamarin:在尝试反序列化对象时出现System.Nullreferenceexception

时间:2013-12-16 13:36:12

标签: c# android xamarin.android xamarin

我尝试反序列化对象并获取System.NullReferenceException。我无法找到异常被触发的地方以及异常的原因。

我尝试像这样反序列化:

public static List<T> Get<T>()
{
    string path = GetFilePath (typeof(T));
    List<T> list;
    using (StreamReader stream = new StreamReader (path)) 
    {
        XmlSerializer serializer = new XmlSerializer (typeof(List<T>));                     
        list = (List<T>)serializer.Deserialize (stream);                        
    }
    return list;
}

栈跟踪

  

在System.Xml.Serialization.XmlSerializationReader.OnUnknownNode上   (System.Xml.XmlNode节点,System.Object o,System.String qnames)   [0x00000] in:0 at   System.Xml.Serialization.XmlSerializationReader.UnknownNode   (System.Object o,System.String qnames)[0x00000] in:0 at   System.Xml.Serialization.XmlSerializationReader.UnknownNode   (System.Object o)[0x00000] in:0 at   System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadListElement   (System.Xml.Serialization.XmlTypeMapping typeMap,Boolean isNullable,   System.Object list,Boolean canCreateInstance)[0x00000] in:0 at   System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject   (System.Xml.Serialization.XmlTypeMapping typeMap,Boolean isNullable,   Boolean checkType)[0x00000] in:0 at   System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot   (System.Xml.Serialization.XmlTypeMapping rootMap)[0x00000] in   :0点   System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot()   [0x00000] in:0 at   System.Xml.Serialization.XmlSerializer.Deserialize   (System.Xml.Serialization.XmlSerializationReader reader)[0x00000] in   :0

1 个答案:

答案 0 :(得分:1)

您粘贴的XML无效。我尝试将其粘贴到记事本中,保存为test.xml并在Chrome中打开。看起来它不喜欢分号。我带他们离开我,下面打开了很好:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfBusinessOffer xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema">
<BusinessOffer>
</BusinessOffer>
</ArrayOfBusinessOffer>