rongfengliang / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Serialize with XmlWriter #432

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
1. How to a serialize a class into an xml file with protobuf-net
My output file is always blank after serialization.
2.
3.

What is the expected output? What do you see instead?
Can we have an example of an xml serialization of a class in C#

What version of the product are you using? On what operating system?
r668

Please provide any additional information below.
Class Test
<code>
    [ProtoContract, Serializable]
    class ClassSTest : IXmlSerializable
    {
        [ProtoMember(1)]
        string toto = "Toto";

        public void Serial()
        {
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
            {
                ConformanceLevel = ConformanceLevel.Fragment
            };
            Serializer.Serialize<ClassSTest>(XmlWriter.Create("toto.xml", xmlWriterSettings), this);
        }

        XmlSchema IXmlSerializable.GetSchema() { return null; }
        void IXmlSerializable.ReadXml(XmlReader reader) { Serializer.Merge(reader, this); }
        void IXmlSerializable.WriteXml(XmlWriter writer) { Serializer.Serialize(writer, this); }
    }
</code>

Original issue reported on code.google.com by harold.p...@gmail.com on 27 Feb 2014 at 3:03

GoogleCodeExporter commented 9 years ago
You didn't properly close the writer. Try:

    using(var writer = XmlWriter.Create("toto.xml", xmlWriterSettings))
    {
        Serializer.Serialize<ClassSTest>(writer, this);
    }

Note, however, that protobuf-net *is not xml*; the xml here is intended for 
some specific use-cases for systems that only allow xml, but it is actually 
just a hunk of raw data in the output. protobuf-net is not an xml formatter.

Original comment by marc.gravell on 27 Feb 2014 at 5:50

GoogleCodeExporter commented 9 years ago
Ah OK.
I thought it was also an xml formatter.
It would have been useful for my project.

Thank you anyway.
Protobuf-net is great.

Original comment by harold.p...@gmail.com on 3 Mar 2014 at 10:36