EDN Admin
Well-known member
Im trying to save a custom list to a XML file, then bring it back. It crashes in the deserialization. Any ideas? My real simple sample worked, but this doesnt.
Crash in LoadList_1()--marked in sample code with //crash
System.InvalidOperationException was unhandled
Message=There is an error in XML document (47, 21).
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at Sorted_Checklist.Sorted_CheckList.LoadList_1()
at Sorted_Checklist.Sorted_CheckList.btnLoad_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
InnerException: System.Xml.XmlException
Message=Data at the root level is invalid. Line 47, position 21.
LineNumber=47
LinePosition=21
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadEndElement()
at System.Xml.Serialization.XmlSerializationReader.deserializeLiteralArray(Accessor accessor, LogicalType deserializeAs, Fixup fixup, Object fixupTarget, Boolean isNull)
at System.Xml.Serialization.XmlSerializationReader.DeserializeArrayLikeElement(LogicalType deserializeAs, Accessor accessor, Fixup fixup, Object fixupTarget, String identifier, Boolean emptyValue, Boolean nullValue)
at System.Xml.Serialization.XmlSerializationReader.deserializeElement(Accessor accessor, Fixup fixup, Object fixupTarget)
at System.Xml.Serialization.XmlSerializationReader.DeserializeElement(Accessor accessor, Fixup fixup)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at Sorted_Checklist.Sorted_CheckList.LoadList_1()
at Sorted_Checklist.Sorted_CheckList.btnLoad_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
Code snippet
private void LoadList_1()
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = storageFile.OpenFile("List_1.xml", FileMode.Open))
{
if (stream.Length > 0)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<MyCheckBox>));
List<MyCheckBox> data = (List<MyCheckBox>)serializer.Deserialize(stream); //crash
//
MySortedList_1 = data;
DataContext = MySortedList_1;
}
}
}
private void SaveList_1()
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile.OpenFile("List_1.xml", FileMode.OpenOrCreate))
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Sorted_Checklist.Sorted_CheckList.MyCheckBox>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, this.MySortedList_1);
}
}
catch
{
}
}
}
public class MyCheckBox:IComparable
{
public MyCheckBox() { }
public MyCheckBox(string title, bool ischecked)
{
Title = title;
IsChecked = ischecked;
}
public string Title { get; set; }
public bool IsChecked { get; set; }
int IComparable.CompareTo(object obj)
{
MyCheckBox temp = (MyCheckBox)obj;
if (temp != null)
return this.Title.CompareTo(temp.Title);
else throw new ArgumentException("Parameter is not a title of a checkbox!");
}
}
<hr class="sig Rick Hantz
View the full article
Crash in LoadList_1()--marked in sample code with //crash
System.InvalidOperationException was unhandled
Message=There is an error in XML document (47, 21).
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at Sorted_Checklist.Sorted_CheckList.LoadList_1()
at Sorted_Checklist.Sorted_CheckList.btnLoad_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
InnerException: System.Xml.XmlException
Message=Data at the root level is invalid. Line 47, position 21.
LineNumber=47
LinePosition=21
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadEndElement()
at System.Xml.Serialization.XmlSerializationReader.deserializeLiteralArray(Accessor accessor, LogicalType deserializeAs, Fixup fixup, Object fixupTarget, Boolean isNull)
at System.Xml.Serialization.XmlSerializationReader.DeserializeArrayLikeElement(LogicalType deserializeAs, Accessor accessor, Fixup fixup, Object fixupTarget, String identifier, Boolean emptyValue, Boolean nullValue)
at System.Xml.Serialization.XmlSerializationReader.deserializeElement(Accessor accessor, Fixup fixup, Object fixupTarget)
at System.Xml.Serialization.XmlSerializationReader.DeserializeElement(Accessor accessor, Fixup fixup)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at Sorted_Checklist.Sorted_CheckList.LoadList_1()
at Sorted_Checklist.Sorted_CheckList.btnLoad_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
Code snippet
private void LoadList_1()
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = storageFile.OpenFile("List_1.xml", FileMode.Open))
{
if (stream.Length > 0)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<MyCheckBox>));
List<MyCheckBox> data = (List<MyCheckBox>)serializer.Deserialize(stream); //crash
//
MySortedList_1 = data;
DataContext = MySortedList_1;
}
}
}
private void SaveList_1()
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile.OpenFile("List_1.xml", FileMode.OpenOrCreate))
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Sorted_Checklist.Sorted_CheckList.MyCheckBox>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, this.MySortedList_1);
}
}
catch
{
}
}
}
public class MyCheckBox:IComparable
{
public MyCheckBox() { }
public MyCheckBox(string title, bool ischecked)
{
Title = title;
IsChecked = ischecked;
}
public string Title { get; set; }
public bool IsChecked { get; set; }
int IComparable.CompareTo(object obj)
{
MyCheckBox temp = (MyCheckBox)obj;
if (temp != null)
return this.Title.CompareTo(temp.Title);
else throw new ArgumentException("Parameter is not a title of a checkbox!");
}
}
<hr class="sig Rick Hantz
View the full article