无法使XML(de)序列化程序正常工作

时间:2015-09-24 08:44:03

标签: c# xml unity3d xml-parsing

我自己做了一个快速的挑战;制作通用的XML(反)序列化程序。我遇到了I / O部分的问题。代码如下:

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;
    }
}

可能是我做了一些疯狂的菜鸟错误,但我根本看不到它。

从另一个脚本调用这些方法,并在我知道设置正确的类上调用。

澄清:

如果我序列化然后反序列化相同的数据,我会收到如下错误:

  

IOException:在路径上共享违规   C:\开发\ C#\项目\ XML \资产\ leaderboard.xml   System.IO.FileStream..ctor(System.String路径,FileMode模式,   FileAccess访问,FileShare共享,Int32 bufferSize,布尔值   匿名,FileOptions选项)(at   /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:320)   System.IO.FileStream..ctor(System.String路径,FileMode模式,   FileAccess访问,FileShare共享,Int32 bufferSize)(包装器   remoting-invoke-with-check)System.IO.FileStream:.ctor   (字符串,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)(在Assets / XMLLoader.cs:31)   XML.SerializeXML [排行榜](.Leaderboard obj,System.String   xmlPath,Boolean append)(在Assets / XMLLoader.cs:25)XMLTest.Start()   (在Assets / XMLTest.cs:14)

2 个答案:

答案 0 :(得分:2)

我认为这是一个简单的疏忽。您正在尝试创建该文件(如果存在)!只需扭转条件就可以了:

if (!File.Exists(GetFullPath(relativePath)))
{
    File.Create(GetFullPath(relativePath));
}

答案 1 :(得分:1)

为了澄清这是我用于序列化/反序列化xml的内容。

也许通过修改它会对你有帮助。

/// <summary>
/// Class containing a load and store method to store a datafile as XML format.
/// </summary>
/// <typeparam name="T">The root object containing the data</typeparam>
public class DataFile<T> where T : new()
{
    /// <summary>
    /// Load the XML from Path
    /// </summary>
    /// <param name="path">Path to a XML file containing the data for object of type T</param>
    /// <returns>An object of type T as represented by the XML. Will return default(T) when the </returns>
    public static T Load(string path)
    {
        if (!File.Exists(path)) return default(T);
        var ser = new XmlSerializer(typeof(T));
        using (var stream = File.OpenRead(path))
        {
            var reader = new XmlTextReader(stream);
            if (!ser.CanDeserialize(reader))
                return default(T); // return null indicating the file can not be loaded

            return (T) ser.Deserialize(reader);
        }
    }

    /// <summary>
    /// Stores an object of type T as XML into a file
    /// </summary>
    /// <param name="path">A path to a file which should be written.</param>
    /// <param name="data">The object to be written to the given file</param>
    public static void Store(string path, T data)
    {
        var ser = new XmlSerializer(typeof(T));
        using (var stream = File.OpenWrite(path))
        {
            // Clear the file and write new contents.
            stream.SetLength(0);
            ser.Serialize(stream, data);
        }
    }
}