美文网首页unity3D技术分享Unity教程合集
C#泛型XML序列化和反序列化

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

作者: 2b75747cf703 | 来源:发表于2016-11-15 16:47 被阅读352次

样例:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfBlendShapeFrame xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <BlendShapeFrame>
        <BlendShape name="blend_kaixin" weight="0" />
        <BlendShape name="blend_daxiao" weight="0" />
        <BlendShape name="blend_O" weight="0" />
        <BlendShape name="blend_o" weight="0" />
    </BlendShapeFrame>

    <BlendShapeFrame>
        <BlendShape name="blend_kaixin" weight="6.1" />
        <BlendShape name="blend_daxiao" weight="0" />
        <BlendShape name="blend_O" weight="6.8" />
        <BlendShape name="blend_o" weight="0" />
    </BlendShapeFrame>

    <BlendShapeFrame>
        <BlendShape name="blend_kaixin" weight="16.8" />
        <BlendShape name="blend_daxiao" weight="0" />
        <BlendShape name="blend_O" weight="18.6" />
        <BlendShape name="blend_o" weight="0" />
    </BlendShapeFrame>
</ArrayOfBlendShapeFrame>
using System.Collections.Generic;
using System.Xml.Serialization;

public class BlendShapeFrame
{
    [XmlAttribute]
    public float time;

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

public class BlendShape
{
    [XmlAttribute]
    public string name;

    [XmlAttribute]
    public float weight;
}
var keyframes = XmlUtility.DeserializeXml<List<BlendShapeFrame>>(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<T>(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<T>(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<T>(string path)
        {
            if (!File.Exists(path))
                return default(T);

            string text = File.ReadAllText(path);

            return DeserializeXml<T>(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序列化和反序列化

    本文链接:https://www.haomeiwen.com/subject/epccpttx.html