Using generics with XmlSerializer

后端 未结 5 1036
醉梦人生
醉梦人生 2020-12-29 08:16

When using XML serialization in C#, I use code like this:

public MyObject LoadData()
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyObject));         


        
相关标签:
5条回答
  • 2020-12-29 08:42

    A simple generic wrapper:

    public class GenericSerializer<T> : XmlSerializer
    {
        public GenericSerializer(): base(typeof(T)) { }
    }
    

    This will serialize your object to the bin/debug folder:

    static void Main(string[] args)
    {
       Person p = new Person { Name = "HelloWorld" };
       GenericSerializer<Person> ser = new GenericSerializer<Person>();
       ser.Serialize(new StreamWriter("person.xml"), p);
    }
    
    0 讨论(0)
  • 2020-12-29 08:42

    Try this.

    public class SerializeConfig<T> where T : class
    {
        public static void Serialize(string path, T type)
        {
            var serializer = new XmlSerializer(type.GetType());
            using (var writer = new FileStream(path, FileMode.Create))
            {
                serializer.Serialize(writer, type);
            }
        }
    
        public static T DeSerialize(string path)
        {
            T type;
            var serializer = new XmlSerializer(typeof(T));
            using (var reader = XmlReader.Create(path))
            {
                type = serializer.Deserialize(reader) as T;
            }
            return type;
        }
    }
    
    0 讨论(0)
  • 2020-12-29 08:43

    Make your serialization class/method generic:

    public T LoadData<T>()
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        using (TextReader reader = new StreamReader(settingsFileName))
        {
            return (T)xmlSerializer.Deserialize(reader);
        }
    }
    
    0 讨论(0)
  • 2020-12-29 08:47

    always work's for me

      public static string ObjectToXmlSerialize<T>(T dataToSerialize)
        {
            try
            {
                var stringwriter = new System.IO.StringWriter();
                var serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(stringwriter, dataToSerialize);
                return stringwriter.ToString();
            }
            catch (Exception ex)
            {
            }
            return null;
        }
    

    and this is for Deserialize:

      public static T XmlDeserializeToObject<T>(string xmlText)
        {
            try
            {
                var stringReader = new System.IO.StringReader(xmlText);
                var serializer = new XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(stringReader);
            }
            catch (Exception ex)
            {
            }
            return default(T);
        }
    
    0 讨论(0)
  • 2020-12-29 08:55

    An addition to @Oded, you can make the method Generic aswell:

    public T ConvertXml<T>(string xml)
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(new StringReader(xml));
    }
    

    This way you don't need to make the whole class generic and you can use it like this:

    var result = ConvertXml<MyObject>(source);
    
    0 讨论(0)
提交回复
热议问题