首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >往返于XPathDocument的协议缓冲区消息

往返于XPathDocument的协议缓冲区消息
EN

Stack Overflow用户
提问于 2011-06-24 12:25:22
回答 1查看 753关注 0票数 1

我正在尝试序列化和反序列化XPathDocument之间的协议缓冲区消息,但是它失败了,只有一个例外:ProtoBuf.ProtoException: message中检测到的不匹配的组标记--如何使它工作?

我正在使用protobuf-net,我用于复制它的源代码如下所示:

TestMsg.proto

代码语言:javascript
复制
    option optimize_for = SPEED;

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

message A {
    required string str = 1;
}

Progam.cs

代码语言:javascript
复制
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;
        }
    }
}

我试图扩展use,但是当测试对象从Serializer.Merge返回时它是空的。

代码语言:javascript
复制
    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 TestXMLSerilizationLars
{
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            A a = new A();
            string str = "test";
            a.str = str;
            t.a.Add(a);
            XPathDocument xmldoc = Serialize(t);
            WriteXpathDocument(xmldoc, "c:\\testmsg.xml");
            Test t1 = Serialize(xmldoc);
        }

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

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

        public static Test Serialize(XPathDocument xmlDoc)
        {
            Test t = null;

            Type type = xmlDoc.GetType();

            XmlSerializer serializer = new XmlSerializer(type);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                serializer.Serialize(memoryStream, xmlDoc);
                // memoryStream.Close();
                Test newt = Deserialize(memoryStream.ToArray());
                return newt;
            }            
            return t;
        }

        static public Test Deserialize(byte[] Bytes)
        {
            MemoryStream SerializeStream = new MemoryStream(Bytes);
            Test NewObject = Serializer.Deserialize<Test>(SerializeStream);
            Test ObjectExist = new Test();

            if (ObjectExist == null)
            {
                return NewObject;
            }
            else
            {
                SerializeStream.Seek(0, SeekOrigin.Begin);
                Serializer.Merge<Test>(SerializeStream, ObjectExist);
                return ObjectExist;
            }
        }

        public static void WriteXpathDocument(XPathDocument xpathDoc, string filename)
        {
            // Create XpathNaviagtor instances from XpathDoc instance.
            XPathNavigator objXPathNav = xpathDoc.CreateNavigator();

            // Create XmlWriter settings instance.
            XmlWriterSettings objXmlWriterSettings = new XmlWriterSettings();
            objXmlWriterSettings.Indent = true;

            // Create disposable XmlWriter and write XML to file.
            using (XmlWriter objXmlWriter = XmlWriter.Create(filename, objXmlWriterSettings))
            {
                objXPathNav.WriteSubtree(objXmlWriter);
                objXmlWriter.Close();
            }
        }
    }
}

我抛出的xml文件如下所示

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <a>
    <A>
      <str>test</str>
    </A>
  </a>
</Test>
EN

回答 1

Stack Overflow用户

发布于 2011-06-24 22:45:39

您在这里使用protobuf的唯一方法是:

代码语言:javascript
复制
x.Serialize(w, xmlDoc);
memoryStream.Position = 0;
t = Serializer.Deserialize<Test>(memoryStream);

您已经编写了xml (x是一个XmlSerializer),然后尝试通过protobuf (Serializer.Deserialize)读取它。

但是,protobuf不是xml;它是一种与xml完全无关的二进制格式。如果您打算深入克隆数据,还应该使用protobuf (Serializer.Serialize)进行序列化。

通常可以在两种格式之间转换模型,但是流本身是不可交换的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6467758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档