Serialization of existing XML Type

180 views
Skip to first unread message

Shail

unread,
Jul 30, 2012, 1:03:48 AM7/30/12
to prot...@googlegroups.com
As part of using protocol buffer .Net, I Came across that any XMLType can be directly serilaized,
My sample XML Type is :
     [XmlType]
     public class Movie
     {
         [XmlElement("MovieNameValue", Order = 0)]
         public string MovieName;

         [XmlElement("MovieNameValue", Order = 1)]
         public string MovieDir;

         [XmlAttribute("TheaterCount")]
         public int TheatersCount;

     }

When I serialize this I am able to serialize Element Types with order 1 or more, but it does not support serialization of Attribute types.

I assume this order constraint comes from Google Protocol Buffer Spec, that tag number should start from 1.
and attributes does not have order information, so it can not be serialized.
But in my case it is important that exiting XML serializable class does not get modified, Is there any work around for this scenario?
or is it possible to modify existing Protobuf-net code and generate new protobuf-net dlls for usage?

Thanks and Regards
Shailendra

Marc Gravell

unread,
Jul 30, 2012, 1:34:22 AM7/30/12
to Shail, prot...@googlegroups.com
protobuf-net indeed needs *some* way to associate protobuf numbers with members; one of the ways it supports is `[XmlElement(Order=n)]`, but to confirm: yes the "n" needs to be >= 1, and yes, since `[XmlAttribute]` doesn't specify any such number, protobuf-net can't use that in any meaningful way.

One option, of course, is to add `[ProtoContract]` and `[ProtoMember(n)]` attributes; these will take precedence over `[XmlType]` and `[XmlElement]`; but you say you don't want to edit the existing class; fine.

If the reason for this is that `Movie` is a generated type, for example from xsd.exe, then another option might be a `partial class`. With such, you can add a **second** class file that is merged with the existing .cs by the compiler. This is ideal for adding information to a generated type, for example (in a second .cs, and in the same namespace):

    [ProtoContract]
    [ProtoPartialMember(1, "MovieName"),ProtoPartialMember(2, "MovieDir")]
    [ProtoPartialMember(3, "TheatersCount")]
    partial class Movie { }

If, however, even this is not allowed, then you can tell protobuf-net about `Movie` at runtime rather than via attributes; for example (during your apps startup):

    RuntimeTypeModel.Default.Add(typeof(Movie), false).Add("MovieName", "MovieDir", "TheatersCount");

(that is just a very basic usage; there are much more subtle configuration options available)

Any of those help?

Marc
(protobuf-net)

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/osWVVnYwU-cJ.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.



--
Regards,

Marc

Shail

unread,
Jul 30, 2012, 1:44:38 AM7/30/12
to prot...@googlegroups.com, Shail
Thanks Marc...This is what I was looking for...Thanks again   for your support  and quick reply.
To unsubscribe from this group, send email to protobuf+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.



--
Regards,

Marc
Reply all
Reply to author
Forward
0 new messages