I've been doing a quick challenge I set myself; make a generic XML (de)serializer. I'm having trouble with the I/O part of this. Code is as follows:
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;
public static class XML
{
static readonly Dictionary<string, XmlSerializer> PathSerializers = new Dictionary<string, XmlSerializer>();
public static T DeserializeXML<T>(string xmlPath)
{
var serializer = GetSerializer<T>(xmlPath);
var xml = File.ReadAllText(GetFullPath(xmlPath));
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
return (T) serializer.Deserialize(stream);
}
}
public static void SerializeXML<T>(T obj, string xmlPath, bool append)
{
using (var text = new StreamWriter(GetFullPath(xmlPath), append, Encoding.ASCII))
{
GetSerializer<T>(xmlPath).Serialize(text, obj);
}
}
private static XmlSerializer GetSerializer<T>(string relativePath)
{
return PathSerializers.ContainsKey(relativePath) ? PathSerializers[relativePath] : AddSerializer<T>(relativePath);
}
private static string GetFullPath(string relativePath)
{
return Path.Combine(Application.dataPath, relativePath);
}
private static XmlSerializer AddSerializer<T>(string relativePath)
{
if (File.Exists(GetFullPath(relativePath)))
{
File.Create(GetFullPath(relativePath));
}
if (PathSerializers.ContainsKey(relativePath))
{
Debug.Log("Loaded cached serializer");
return GetSerializer<T>(relativePath);
}
var serializer = new XmlSerializer(typeof(T));
PathSerializers.Add(relativePath, serializer);
return serializer;
}
}
It might be me doing some crazy rookie mistake, but I simply cannot see it.
The methods are called from another script, and called on a class that I know is set up correctly.
Clarification:
If I serialize and then deserialize the same data I get an error as follows:
IOException: Sharing violation on path C:\Dev\C#\Projects\XML\Assets\leaderboard.xml System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:320) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int) System.IO.File.Create (System.String path, Int32 bufferSize) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:135) System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:130) XML.AddSerializer[Leaderboard] (System.String relativePath) (at Assets/XMLLoader.cs:43) XML.GetSerializer[Leaderboard] (System.String relativePath) (at Assets/XMLLoader.cs:31) XML.SerializeXML[Leaderboard] (.Leaderboard obj, System.String xmlPath, Boolean append) (at Assets/XMLLoader.cs:25) XMLTest.Start () (at Assets/XMLTest.cs:14)