Here is my code:
public partial class MainWindow : Window
{
List<Cliente> CContent;
string mainPath = @"D:\70-536\Clientes.dat";
public MainWindow()
{
InitializeComponent();
Do();
}
private void Do()
{
FileInfo fi = new FileInfo(mainPath);
if (fi.Exists)
{
CContent = ReturnListOfPersistentFile<Cliente>(mainPath);
}
else
{
CContent = new List<Cliente>();
}
}
public List<T> ReturnListOfPersistentFile<T> (string Filename)
{
SoapFormatter sf = new SoapFormatter();
using (Stream fStream = new FileStream(Filename,FileMode.Open, FileAccess.Read,FileShare.None))
{
List<T> list = new List<T>();
list = (List<T>)sf.Deserialize(fStream);
return list;
}
}
This is the stacktrace of my innerexception:
en System.Xml.XmlTextReaderImpl.Throw(Exception e) en System.Xml.XmlTextReaderImpl.Throw(String res, String arg) en System.Xml.XmlTextReaderImpl.ThrowUnclosedElements() en System.Xml.XmlTextReaderImpl.ParseAttributes() en System.Xml.XmlTextReaderImpl.ParseElement() en System.Xml.XmlTextReaderImpl.ParseDocumentContent() en System.Xml.XmlTextReaderImpl.Read() en System.Xml.XmlTextReader.Read() en System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
en System.Runtime.Serialization.Formatters.Soap.SoapParser.Run() en System.Runtime.Serialization.Formatters.Soap.ObjectReader.Deserialize(HeaderHandler handler, ISerParser serParser) en System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(Stream serializationStream, HeaderHandler handler) en System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(Stream serializationStream) en Solution1.MainWindow.ReturnListOfPersistentFile[T](String Filename) en c:\users\u201114160\documents\visual studio 2010\Projects\Solution1\Solution1\MainWindow.xaml.cs:línea 99 en Solution1.MainWindow.Do() en c:\users\u201114160\documents\visual studio 2010\Projects\Solution1\Solution1\MainWindow.xaml.cs:línea 45
en Solution1.MainWindow..ctor() en c:\users\u201114160\documents\visual studio 2010\Projects\Solution1\Solution1\MainWindow.xaml.cs:línea 36
It drives me crazy, some ideas? This exception was throwed because "ReturnListOfPersistentFile" generic method, but WHY?
Thanks in advance.
ANSWER: I was trying to serialize a generic list with SOAP formatter, but SOAP formatter doesn't support generic lists.
I ran into this thread on forums.microsoft.com. Let me quote what microsoft employee said:
We have decided not to invest in any significant new feature work for the SoapFormatter in Whidbey
Thanks everyone.
Looking at your stacktrace, I notice the method ThrowUnclosedElements. This makes me think that you got a corrupted or ill-formated file.
As yourself have pointed out, SOAP serialization doesn't support generic lists. As I understand, this means that you tried to serialize a List and as result you got a file that cannot be deserialized. This have been discussed before here, and also here (your own cite) among other places.
A solution may be to use a non-generic, old fashion, forgotten ArrayList. Of course you got to access the items and cast them to the appropriate type, this can be done with a wrapper class.
An alternative is to serialize object per object, instead of serializing the list.