0

I try to deserialize object and get System.NullReferenceException. I can't find where exception is fired and what is the cause of exception.

I try to deserialize like this:

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;
}

StackTrace

at System.Xml.Serialization.XmlSerializationReader.OnUnknownNode (System.Xml.XmlNode node, 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 at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00000] in :0 at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00000] in :0

5
  • What does GetFilePath return? What's in the file? Commented Dec 16, 2013 at 13:42
  • GetFilePath returns path of the file. In file is Xml document. Content is: <?xml version="1.0" encoding="utf-8"?> <ArrayOfBusinessOffer xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema"> <BusinessOffer> </BusinessOffer> </ArrayOfBusinessOffer> Commented Dec 16, 2013 at 13:53
  • And are you sure that GetFilePath itself is doing the right thing? Have you debugged through to check that? Commented Dec 16, 2013 at 14:00
  • Yes, I have debugged GetFilePath. I can get the content of xml file like this string text = File.ReadAllText (path). The correct content of xml file without semicolons is <?xml version="1.0" encoding="utf-8"?> <ArrayOfBusinessOffer xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema"> <BusinessOffer> </BusinessOffer> </ArrayOfBusinessOffer> Commented Dec 16, 2013 at 14:21
  • But I continue get System.NullReferenceException Commented Dec 16, 2013 at 14:27

1 Answer 1

1

The XML you've pasted is invalid. I tried pasting it into Notepad, saving as test.xml and opening in Chrome. It looks like it doesn't like the semicolons. I took them out to leave me with the following which opened fine:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfBusinessOffer xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema">
<BusinessOffer>
</BusinessOffer>
</ArrayOfBusinessOffer>
Sign up to request clarification or add additional context in comments.

1 Comment

I continue get System.NullReferenceException. What can be the cause?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.