C#泛型XML序列化和反序列化

样例:



    
        
        
        
        
    

    
        
        
        
        
    

    
        
        
        
        
    

using System.Collections.Generic;
using System.Xml.Serialization;

public class BlendShapeFrame
{
    [XmlAttribute]
    public float time;

    [XmlElement("BlendShape")]
    public List blendShapes;
}

public class BlendShape
{
    [XmlAttribute]
    public string name;

    [XmlAttribute]
    public float weight;
}
var keyframes = XmlUtility.DeserializeXml>(bytes);

XmlUtility

using UnityEngine;
using System.Xml.Serialization;
using System.IO;
using System;
using System.Text;

namespace Babybus.Framework.Serialization
{
    public class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding
        {
            get
            {
                return Encoding.UTF8;
            }
        }
    }

    public class XmlUtility
    {
        public static T DeserializeXml(byte[] bytes)
        {
            T value = default(T);

            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(T));

                using (MemoryStream memoryStream = new MemoryStream(bytes))
                {
                    TextReader textReader = new StreamReader(memoryStream);
                    value = (T)deserializer.Deserialize(textReader);
                    textReader.Close();
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            return value;
        }

        public static T DeserializeXml(string text)
        {
            T value = default(T);

            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(T));
                StringReader reader = new StringReader(text);
                value = (T)deserializer.Deserialize(reader);
                reader.Close();
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            return value;
        }

        public static T DeserializeXmlFromFile(string path)
        {
            if (!File.Exists(path))
                return default(T);

            string text = File.ReadAllText(path);

            return DeserializeXml(text);
        }

        public static string SerializeXml(object o)
        {
            string contents = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(o.GetType());
                using (StringWriter writer = new Utf8StringWriter())
                {
                    serializer.Serialize(writer, o);
                    contents = writer.ToString();
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            return contents;
        }

        public static void SerializeXmlToFile(string path, object o)
        {
            if (string.IsNullOrEmpty(path))
                return;

            string contents = SerializeXml(o);
            if (contents != null)
                File.WriteAllText(path, contents);
        }
    }
}

你可能感兴趣的:(C#泛型XML序列化和反序列化)