public void SerializeObject(string filename, T data)
{
// Get the path of the save game
string fullpath = filename;
// Open the file, creating it if necessary
//if (container.FileExists(filename))
// container.DeleteFile(filename);
FileStream stream = (FileStream)File.Open(fullpath, FileMode.OpenOrCreate);
try
{
// Convert the object to XML data and put it in the stream
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(stream, data); //Thrown HERE
}
finally
{
// Close the file
stream.Close();
}
}
Как я могу сделать вышеуказанный код прекратить металирование InvalidOperationException?XmlSerializer Access Denied
Полное сообщение об ошибке: Невозможно создать временный класс (результат = 1). ошибка CS0016: не удалось записать в выходной файл 'c: \ Users [MYUSERNAME] \ AppData \ Local \ Temp \ czdgjjs0.dll' - 'Доступ запрещен.
Я понятия не имею, как обойти эту ошибку.
Я пытаюсь сериализации моего класса уровня, который выглядит следующим образом:
[Serializable]
public class Level : ISerializable
{
public string Name { get; set; }
public int BestTime { get; set; } //In seconds
public List<Block> levelBlocks { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Level()
{
}
public Level(SerializationInfo info, StreamingContext ctxt)
{
this.Name = (String)info.GetValue("Name", typeof(String));
this.BestTime = (int)info.GetValue("BestTime", typeof(int));
this.levelBlocks = (List<Block>)info.GetValue("Blocks", typeof(List<Block>));
this.Width = (int)info.GetValue("Width", typeof(int));
this.Height = (int)info.GetValue("Height", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Name", this.Name);
info.AddValue("BestTime", this.BestTime);
info.AddValue("Blocks", this.levelBlocks);
info.AddValue("Width", this.Width);
info.AddValue("Height", this.Height);
}
}
Моих блоки класс осуществляются аналогичным образом и содержит только вектор положения, который сохраняется.
Ниже, мой метод сохранения:
public static void Save()
{
string filename = "saved.xml";
Level toSave = new Level();
toSave.levelBlocks = new List<Block>();
//TODO: build toSave
toSave.Name = "This is a level!";
toSave.BestTime = 0;
foreach (Entity e in EntityController.Entities)
{
if (e is Block)
{
toSave.levelBlocks.Add((Block)e);
if (e.Position.X > toSave.Width)
toSave.Width = (int)e.Position.X;
if (e.Position.Y > toSave.Height)
toSave.Height = (int)e.Position.Y;
}
}
serializer.SerializeObject(filename, toSave);
}
Моя программа является XNA игра.
Вы можете указать, как вы называете 'SerializeObject'? – Turbot
уверен, я добавил. – Runewake2
в соответствии с ответом Шелдона, если вы используете ASP.NET, убедитесь, что ваш рабочий процесс IIS имеет разрешение на доступ к tempDirectory, в противном случае предварительно установленный в выделенную папку. – Turbot