protocol buffer message to and from an XPathDocument

139 views
Skip to first unread message

Lars Schouw

unread,
Jun 24, 2011, 10:02:07 AM6/24/11
to prot...@googlegroups.com

I am trying to serialize and deserialize a protocol buffer message to and from an XPathDocument but it fails with an exception: ProtoBuf.ProtoException: Mismatched group tags detected in messageHow do I make this work?

I am using protobuf-net and my source code for reproducing it looks like this:

TestMsg.proto

    option optimize_for = SPEED;

//*************************
message
Test {
    repeated A a
= 1;
}

message A
{
    required
string str = 1;
}

Progam.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;
using ProtoBuf;
using TestMsg;

namespace protocolbufferserialize
{
   
class Program
   
{
       
static void Main(string[] args)
       
{
           
Test t = new Test();
           
XPathDocument xmldoc = Serialize(t);
           
Test t1 = Serialize(xmldoc);
       
}

       
public static XPathDocument Serialize(Test wro)
       
{
           
XPathDocument xmlDoc = null;
           
Serializer.PrepareSerializer<Test>();

           
XmlSerializer x = new XmlSerializer(wro.GetType());
           
using (MemoryStream memoryStream = new MemoryStream())
           
{
               
using (TextWriter w = new StreamWriter(memoryStream))
               
{
                    x
.Serialize(w, wro);
                    memoryStream
.Position = 0;
                    xmlDoc
= new XPathDocument(memoryStream);
               
}
           
}
           
return xmlDoc;
       
}

       
public static Test Serialize(XPathDocument xmlDoc)
       
{
           
Test t = null;
           
Serializer.PrepareSerializer<Test>();

           
XmlSerializer x = new XmlSerializer(xmlDoc.GetType());
           
using (MemoryStream memoryStream = new MemoryStream())
           
{
               
using (TextWriter w = new StreamWriter(memoryStream))
               
{
                    x
.Serialize(w, xmlDoc);
                    memoryStream
.Position = 0;
                    t
= Serializer.Deserialize<Test>(memoryStream);
               
}
           
}
           
return t;
       
}
   
}
}

Marc Gravell

unread,
Jun 24, 2011, 3:29:48 PM6/24/11
to prot...@googlegroups.com
Hi - I'm a bit confused... protobuf is a binary format with no relationship *whatsoever* to XML. It makes no sense to write XML and try to read it as protobuf. What is it you want to do here?
--
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/-/EXMM0ECi3McJ.
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.

lasse

unread,
Jul 20, 2011, 7:18:45 PM7/20/11
to prot...@googlegroups.com
I want to use XPath on my messages so need to move to and from XML.

Siju Varghese

unread,
Jul 20, 2011, 11:30:19 PM7/20/11
to prot...@googlegroups.com
https://github.com/sijuv/protobuf-codec/ supports protobuf<->xml conversions.

On Thu, Jul 21, 2011 at 4:48 AM, lasse <la...@schouw.me> wrote:
I want to use XPath on my messages so need to move to and from XML.

--
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/-/mKbJ7GITXV0J.

Lars Schouw

unread,
Jul 21, 2011, 6:11:27 AM7/21/11
to prot...@googlegroups.com
I am trying to port protobuf-codecto c# now working against protobuf-net.

In Google official Java implementaion I have com.google.protobuf.Message when is the equivalent in protobuf-net?

Lars
Reply all
Reply to author
Forward
0 new messages