jonhmatter
New Member
While triying to Deserialize a XML stream for a second time, Exceptions shows up:A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dllsA first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dllThe first time it deserialize to modify the XML file which does it properly, but the second time just handle the exception and doesnt modify the fileThis is the XMLManager class:\[code\]class SaveManager{ public static Boolean ConsecutiveSavings; //Consecutive Savings(New goes Last)(1/2/3/4/5/New) or InstanceSave (Overlap depending on SaveNum)(1<-New/2/3) public static String XMLFileName; public static int ParalellSaves; public static void defaultSM() { SaveManager.ConsecutiveSavings = true; SaveManager.XMLFileName = "lol.xml"; SaveManager.ParalellSaves = 3; XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Indent = true; IsolatedStorageFile IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); /* if (IsolatedStorage.FileExists(XMLFileName)) return;*/ IsolatedStorageFileStream Stream = IsolatedStorage.OpenFile(XMLFileName, FileMode.Create); XmlSerializer Serializer = new XmlSerializer(typeof(List<SaveState>)); XmlWriter Writer = XmlWriter.Create(Stream, xmlSettings); int x; List<SaveState> Data = http://stackoverflow.com/questions/12632689/new List<SaveState>(); if (!ConsecutiveSavings) for (x = 0; x < ParalellSaves; x++) Data.Add(new SaveState()); Serializer.Serialize(Writer, Data); Stream.Flush(); Stream.Close(); } public static void XMLTEST() { SaveContainer(new SaveState("Guillermo", 1)); SaveContainer(new SaveState("Guillermo2", 2)); SaveContainer(new SaveState("Guillermo3", 3)); SaveContainer(new SaveState("Guillermo4", 4)); deleteXMLContent(2); deleteXMLContent(3); SaveContainer(new SaveState("Guillermo2", 2)); SaveContainer(new SaveState("Guillermo4", 4)); List<SaveState> Data = http://stackoverflow.com/questions/12632689/readXMLContent(); int x; for (x = 0; x < Data.Count; x++) { Debug.WriteLine(Data.ElementAt(x).Username); Debug.WriteLine(Data.ElementAt(x).LastLevel); } } public static void SaveContainer(SaveState INFO) { XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Indent = true; IsolatedStorageFile IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream Stream = IsolatedStorage.OpenFile(XMLFileName, FileMode.OpenOrCreate); XmlSerializer Serializer = new XmlSerializer(typeof(List<SaveState>)); XmlWriter Writer = XmlWriter.Create(Stream, xmlSettings); try { List<SaveState> Data = (List<SaveState>)Serializer.Deserialize(Stream); if (ConsecutiveSavings) { INFO.SaveNum = Data.Count + 1; Data.Add(INFO); } else { Data.RemoveAt(INFO.SaveNum - 1); Data.Insert(INFO.SaveNum - 1, INFO); } Serializer.Serialize(Writer, Data); } catch { } Stream.Flush(); Stream.Close(); } public static List<SaveState> readXMLContent() { IsolatedStorageFile IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream Stream = IsolatedStorage.OpenFile(XMLFileName, FileMode.Open); XmlSerializer Serializer = new XmlSerializer(typeof(List<SaveState>)); try { List<SaveState> Data = (List<SaveState>)Serializer.Deserialize(Stream); Stream.Flush(); Stream.Close(); return (Data); } catch { } Stream.Flush(); Stream.Close(); return (null); }}\[/code\]and the XML Class :\[code\]public class SaveState{ //DO NOT ERASE, USED BY SAVEMANAGER public int SaveNum; public Boolean Empty; //DO NOT ERASE, USED BY SAVEMANAGER public String Username; public int LastLevel; public SaveState(String auxUsername, int auxLastLevel) { Username = auxUsername; LastLevel = auxLastLevel; Empty = false; } public SaveState() { Empty = true; }}\[/code\]The test Im running is to execute:\[code\] void Main() { SaveManager.defaultSM(); SaveManager.XMLTEST(); }\[/code\]The Exception is generated at public static void SaveContainer(SaveState INFO):\[code\]List<SaveState> Data = http://stackoverflow.com/questions/12632689/(List<SaveState>)Serializer.Deserialize(Stream);\[/code\]Exception which cause a nullPointerException at public static void XMLTEST():\[code\]for (x = 0; x < Data.Count; x++)\[/code\]due to not being able to read the XML stream at:\[code\]List<SaveState> Data = http://stackoverflow.com/questions/12632689/readXMLContent();\[/code\]because readXMLContent(); generates the same exception and returns a NULL;