【Serialization】Xml Serialize Helper

前端之家收集整理的这篇文章主要介绍了【Serialization】Xml Serialize Helper前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
using System.IO;
using System.Xml.Serialization;

namespace Microsoft.PatchCreationService.Utilities
{
    public static class XmlSerializeHelper
    {
        public static void Serialize<T>(string fileName,T target)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (FileStream stream = new FileStream(fileName,FileMode.OpenOrCreate))
            {
                xmlSerializer.Serialize(stream,target);
            };
        }

        public static T Deserialize<T>(string fileName)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (FileStream stream = new FileStream(fileName,FileMode.OpenOrCreate))
            {
                return (T)xmlSerializer.Deserialize(stream);
            };
        }
    }
}


And if you want to ignore some fields when serializing an object,please put an "[XmlIgnore]" attribute above the fields.

For example:

[ XmlIgnore ]
public string NewFilePath;
原文链接:https://www.f2er.com/xml/299884.html

猜你在找的XML相关文章