1

Maybe i misunderstood something for serialization. i wanna serialize my .net object fastest way. i made some googling i found protobuf. Myfirstquestion is ProtoBuf.Net has avility for xml serailization.if it has, can i use it for xml serialization.

My model:


    [XmlType]
public class CT {
    [XmlElement(Order = 1)]
    public int Foo { get; set; }
}
[XmlType]
public class TE {
    [XmlElement(Order = 1)]
    public int Bar { get; set; }
}
[XmlType]
public class TD {
    [XmlElement(Order = 1)]
    public List CTs { get; set; }
    [XmlElement(Order = 2)]
    public List TEs { get; set; }
    [XmlElement(Order = 3)]
    public string Code { get; set; }
    [XmlElement(Order = 4)]
    public string Message { get; set; }
    [XmlElement(Order = 5)]
    public DateTime StartDate { get; set; }
    [XmlElement(Order = 6)]
    public DateTime EndDate { get; set; }
}

my serializer :

            CT ct = new CT() { Foo = 1 };
        List<CT> list = new List<CT>();
        list.Add(ct);
        TE te = new TE() { Bar = 2 };
        List<TE> list2 = new List<TE>();
        list2.Add(te);
        TD td = new TD() { Code = "Test",Message = "Yusuf",StartDate = DateTime.Now,EndDate = DateTime.Now,CTs = list,TEs = list2 };
        List<TD> list3 = new List<TD>();
        list3.Add(td);
        Stopwatch stopwatch5 = new Stopwatch();
        stopwatch5.Start();

        string str = String.Empty;
             using (MemoryStream stream = new MemoryStream()) 
        {
            byte[] data =  Serialize(list3);
            XmlDocument doc = new XmlDocument();
            string xml = Encoding.UTF8.GetString(data); <--SHOULD CREATE XML
            doc.LoadXml(xml);
           // str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length); 
        } 
        stopwatch5.Stop();
        Console.WriteLine(((double)(stopwatch5.Elapsed.TotalMilliseconds * 1000000) / 1000000).ToString("0.00 ns"));
        Console.Read();

    }
    public static byte[] Serialize(List<TD> tData) {
        using(var ms = new MemoryStream()) {
            ProtoBuf.Serializer.Serialize(ms,tData);
            return ms.ToArray();
        }
    }

    public static List<TD> Deserialize(byte[] tData) {
        using(var ms = new MemoryStream(tData)) {
            return ProtoBuf.Serializer.Deserialize<List<TD>>(ms);
        }
    }

it should create xml as a result of " string xml = Encoding.UTF8.GetString(data);". But doesn't. How can i produxe xml result?

Penguen
  • 16,836
  • 42
  • 130
  • 205

2 Answers2

3

Protocol buffers doesn't serialize objects to XML.
It produces binary data. And it has its own set of attributes.
Check this answer

Is Protobuf-net's serialization/deserialization faster than XML ? Yes, by far.
However XmlSerializer is fast enough for most of the tasks. What you should remind when using it though, is:

  • XmlSerializer instance is generating code on the fly and compile this code into an assembly.
  • This generated assembly is then used to serialize and deserialize your objects really fast.
  • So you should cache instances of XmlSerializer (and avoid recreating them)
  • you could add a warm up by calling Serialize and Deserialize in order to initialize inner objects and jit them.

You could even go further by generating the auto-generated assembly by yourself, but then you should remember to regenerate each time you change the objects (It can be automated with a MsBuild Task). You can also look further optimizations:

Community
  • 1
  • 1
Fab
  • 14,327
  • 5
  • 49
  • 68
0

You can only have one tag at the root level of your xml. So either TD cannot be a list, or you must have an outer tag around the List. This code works

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TD td = new TD()
            {
                Code = "Test",
                Message = "Yusuf",
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                CTs = new List<CT>() {
                    new CT() { Foo = 1},
                    new CT() { Foo = 2},
                    new CT() { Foo = 3},
                },
                TEs = new List<TE>() {
                    new TE() { Bar = 1},
                    new TE() { Bar = 2},
                    new TE() { Bar = 3},
                }
            };

           using (MemoryStream stream = new MemoryStream()) 
            {
                byte[] data =  Serialize(td);
                XmlDocument doc = new XmlDocument();
                string xml = Encoding.UTF8.GetString(data); 
                doc.LoadXml(xml);
               // str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length); 
            } 

        }
        public static byte[] Serialize(TD tData)
        {
            using (var ms = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TD));
                serializer.Serialize(ms, tData);
                return ms.ToArray();
            }
        }

        public static TD Deserialize(byte[] tData)
        {
            using (var ms = new MemoryStream(tData))
            {
                XmlSerializer xs = new XmlSerializer(typeof(TD));
                return (TD)xs.Deserialize(ms);
            }
        }
    }
    [XmlRoot("CT")]
    public class CT
    {
        [XmlElement(ElementName = "Foo", Order = 1)]
        public int Foo { get; set; }
    }
    [XmlRoot("TE")]
    public class TE
    {
        [XmlElement(ElementName = "Bar", Order = 1)]
        public int Bar { get; set; }
    }
    [XmlRoot("TD")]
    public class TD
    {
        [XmlElement(ElementName = "CTs",  Order = 1)]
        public List<CT> CTs { get; set; }
        [XmlElement(ElementName = "TEs", Order = 2)]
        public List<TE> TEs { get; set; }
        [XmlElement(ElementName = "Code", Order = 3)]
        public string Code { get; set; }
        [XmlElement(ElementName = "Message", Order = 4)]
        public string Message { get; set; }
        [XmlElement(ElementName = "StartDate", Order = 5)]
        public DateTime StartDate { get; set; }
        [XmlElement(ElementName = "EndDate", Order = 6)]
        public DateTime EndDate { get; set; }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20