It’s my class for save my XML file, i do everythink how you say, and it’s out me exception on IsolatedStorageFileStream at CanSeek NullReferencesException … . it’s on visual studio 2013 community, on visual studio 2015 it’s not out this exception. Help please too understand it.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.IO;
namespace GameSaveMe
{
internal class SaveArgsGame
{
private int id = 0;
private GradeGame grade;
private List items;
private int precentage = 0;
private int overallPrecentage = 0;
private int itemsLevel = 0;
private string date;
public SaveArgsGame(int id, GradeGame grade, List<ItemsName> items, int precentage, int overallPrecentage, int itemsLevel, string date)
{
this.id = id;
this.grade = grade;
this.items = items;
this.precentage = precentage;
this.overallPrecentage = overallPrecentage;
this.itemsLevel = itemsLevel;
this.date = date;
}
internal int ID
{
get
{
return id;
}
}
internal GradeGame Grade
{
get
{
return this.grade;
}
}
internal List<ItemsName> Items
{
get
{
return this.items;
}
}
internal int Precentage
{
get
{
return this.precentage;
}
}
internal int OverallPrecentage
{
get
{
return this.overallPrecentage;
}
}
internal int ItemsLevel
{
get
{
return this.itemsLevel;
}
}
internal string Date
{
get
{
return date;
}
}
}
internal class SaveGame
{
private string file = "Quizzical.xml";
private int idLoad = 1;
public SaveGame()
{
}
internal int ID
{
get { return this.idLoad; }
set { this.idLoad = value; }
}
internal SaveArgsGame Load(int id)
{
//SaveArgsGame game = new SaveArgsGame();
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
XDocument document = OpenXML(iso);
}
return null;
}
internal void LoadFull(List<SaveArgsGame> argsList)
{
argsList.Clear();
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(file))
{
XDocument document = OpenXML(iso);
foreach (var d in document.Root.Element("Game").Elements("Play"))
{
List<ItemsName> items = new List<ItemsName>();
foreach (XElement e in d.Elements())
{
items.Add((ItemsName)Enum.Parse(typeof(ItemsName), e.Value, true));
//System.Diagnostics.Debug.WriteLine(e.Value);
}
argsList.Add(new SaveArgsGame(
Convert.ToInt32(d.Attribute("ID").Value),
(GradeGame)Enum.Parse(typeof(GradeGame), d.Attribute("Game").Value, true),
items,
Convert.ToInt32(d.Attribute("Precentage").Value),
Convert.ToInt32(d.Attribute("OverallPrecentage").Value),
Convert.ToInt32(d.Attribute("ItemsLevel").Value),
d.Attribute("Date").Value));
//System.Diagnostics.Debug.WriteLine("id{0},");
//argsList.Add(new SaveArgsGame(1, GradeGame.MyChoice, new List<ItemsName>() { ItemsName.Cask }, 90, 90, 5));
}
}
}
}
internal void Save(int itemsLevel, int precentage)
{
//SaveArgsGame game = new SaveArgsGame();
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(file))
{
XDocument document = OpenXML(iso);
document = AffixGame(document, itemsLevel, precentage);
document = SortGame(document);
SaveXML(iso, document, FileMode.OpenOrCreate);
}
}
}
internal void SaveSettings(FlickButtonSound settings)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(file))
{
XDocument document = OpenXML(iso);
document.Root.Element("Settings").Attribute("Sound").SetValue(settings.Sound);
document.Root.Element("Settings").Attribute("Effects").SetValue(settings.Effects);
SaveXML(iso, document, FileMode.Create);
System.Diagnostics.Debug.WriteLine("settings saved!!!");
}
}
}
internal void LoadSettings(FlickButtonSound settings)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(file))
{
XDocument document = OpenXML(iso);
settings.Sound = (EnumFlickButtonSound)Enum.Parse(typeof(EnumFlickButtonSound), document.Root.Element("Settings").Attribute("Sound").Value, true);
settings.Effects = (EnumFlickButtonSound)Enum.Parse(typeof(EnumFlickButtonSound), document.Root.Element("Settings").Attribute("Effects").Value, true);
System.Diagnostics.Debug.WriteLine("save load {0}",(EnumFlickButtonSound)Enum.Parse(typeof(EnumFlickButtonSound), document.Root.Element("Settings").Attribute("Sound").Value, true));
System.Diagnostics.Debug.WriteLine("save load {0}",(EnumFlickButtonSound)Enum.Parse(typeof(EnumFlickButtonSound), document.Root.Element("Settings").Attribute("Effects").Value, true));
}
}
}
private XDocument AffixGame(XDocument document, int itemsLevel, int precentage)
{
foreach (var d in document.Root.Element("Game").Elements("Play"))
{
if ((int)d.Attribute("ID") == ID)
{
d.Attribute("ItemsLevel").SetValue(itemsLevel);
d.Attribute("Precentage").SetValue(precentage);
d.Attribute("OverallPrecentage").SetValue((int)d.Attribute("OverallPrecentage") + precentage);
}
}
return document;
}
internal void New(GradeGame game, List<ItemsName> items)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(file))
{
XDocument document = OpenXML(iso);
document = NewGame(document, game, items);
document = ClearOutOfRange(document);
document = SortGame(document);
//AffixGame(document);
SaveXML(iso, document, FileMode.OpenOrCreate);
}
}
}
private XDocument SortGame(XDocument document)
{
XElement[] sortElements = document.Root.Element("Game").Elements("Play")
.OrderByDescending(item => (int)item.Attribute("ItemsLevel")).ToArray();
document.Root.Element("Game").RemoveNodes();
int count = 1;
foreach (XElement el in sortElements)
{
el.Attribute("ID").SetValue(count);
++count;
}
document.Root.Element("Game").Add(sortElements);
return document;
}
private XDocument ClearOutOfRange(XDocument document)
{
if (document.Root.Element("Game").Descendants("Play").Count() > 8)
document.Root.Element("Game").LastNode.Remove();
return document;
}
internal void DeleteByID(int id)
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(file))
{
XDocument document = OpenXML(iso);
List<XElement> deleteElements = document.Root.Element("Game").Elements("Play").ToList();
document.Root.Element("Game").RemoveNodes();
int index = 0;
for (int x = 0; x < deleteElements.Count; x++)
{
if ((int)deleteElements[x].Attribute("ID") == id)
{
index = x;
}
}
deleteElements.RemoveAt(index);
document.Root.Element("Game").Add(deleteElements);
document = SortGame(document);
System.Diagnostics.Debug.WriteLine(" new delete {0}", document);
SaveXML(iso, document, FileMode.Create);
}
}
}
private XDocument NewGame(XDocument document, GradeGame game, List<ItemsName> items)
{
ID = document.Root.Element("Game").Descendants("Play").Count() + 1;
XElement newGameElement = new XElement("Play",
new XAttribute("ID", ID),
new XAttribute("Game", game),
new XAttribute("ItemsLevel", 1),
new XAttribute("Precentage", 0),
new XAttribute("OverallPrecentage", 0),
new XAttribute("Date", DateTime.Now.Date),
new XAttribute("Time", DateTime.Now.ToLocalTime()),
""
);
foreach (var i in items)
newGameElement.Add(new XElement("Item", i));
document.Root.Element("Game").Add(newGameElement);
return document;
}
private XDocument OpenXML(IsolatedStorageFile iso)
{
using (IsolatedStorageFileStream stream = iso.OpenFile(file, FileMode.Open, FileAccess.Read))
{
XDocument document = XDocument.Load(stream);
//stream.Close();
System.Diagnostics.Debug.WriteLine("OpenXML: {0}", document);
return document;
}
}
private void SaveXML(IsolatedStorageFile iso, XDocument document, FileMode mode)
{
using (IsolatedStorageFileStream stream = iso.OpenFile(file, mode, FileAccess.Write))
{
document.Save(stream);
// stream.Close();
System.Diagnostics.Debug.WriteLine("SaveXML: {0}", document);
}
}
internal void Availability()
{
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!iso.FileExists(file))
{
XDocument document = TemplateXML(iso);
SaveXML(iso, document, FileMode.CreateNew);
System.Diagnostics.Debug.WriteLine("ExistsXML: {0}", document);
}
}
}
private XDocument TemplateXML(IsolatedStorageFile iso)
{
XDocument document = new XDocument(
new XElement("Quizzical",
new XElement("Game", ""
),
new XElement("Settings",
new XAttribute("Sound","Sound"),
new XAttribute("Effects", "Effects"))
));
System.Diagnostics.Debug.WriteLine("Created new File");
System.Diagnostics.Debug.WriteLine("{0}", document);
return document;
}
}
}