序列化协议缓存文件转换成XML /文本格式 [英] Serialize protocol buffer file into xml/text format

查看:178
本文介绍了序列化协议缓存文件转换成XML /文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用协议缓冲区.NET 的http:// code .google.com / P / protobuf网/

I am using protocol buffer in .net http://code.google.com/p/protobuf-net/.

我安装了Visual Studio支持的版本,我可以只写原文件中的项目,它会自动生成CSHARP类文件。

I installed the visual studio support version, which I can just write proto file in project and it generates csharp class files automatically.

很多,我需要转储文件到XML(或其他文本格式,如果可用)文件时间。我发现,有一种方法Serializer.Serialize(),它接受一个的XmlWriter参数。我试图使用它,但它抱怨说,我所定义的protobuf的类型必须转换为system.Xml.Serialization.IXmlSerializable。

A lot of times that I need to dump the files into xml(or another text format if available) file. I found that there is a method Serializer.Serialize() which takes an XmlWriter parameter. I tried to use it but it complains that the protobuf type I defined must be convertible to system.Xml.Serialization.IXmlSerializable.

在我的情况,我该怎么做,才能为我喜欢的类型可以转换为System.Xml.Serialization.IXmlSerializable?我不想直接改变CS文件,因为它是在飞行时产生的原文件被更改。

In my case, what I should do in order for my type can be convertible to System.Xml.Serialization.IXmlSerializable? I don't want to change the cs file directly since it is generated on the fly when the proto file is changed.

感谢。

推荐答案

protobuf网的不写的XML;该API是为了让你写的protobuf数据中的一个XML流不透明BLOB(基64)。然而,protobuf网通常很乐意让并排侧的XmlSerializer 用途 - 它尊重大多数相同的隐喻。最有可能的,只要使用新的XmlSerializer(typeof运算(YourRootType))来序列化对象将正常工作。事实上,code代从.proto内部处理的一部分的依赖的这一双重性。

Protobuf-net does not write xml; that API is intended to allow you to write protobuf data as an opaque BLOB (base-64) within an xml stream. However, protobuf-net is usually very happy to allow side-by-side use with XmlSerializer - it respects most of the same metaphors. Most likely, simply using new XmlSerializer(typeof(YourRootType)) to serialize your object will work fine. In fact, part of the internal processing for code-generation from .proto relies on this duality.

如果你想的明确的在你产生code XML标记(即 [XmlType将(...)]等)只需使用号码:XML 命令行选项,该选项(如果您使用的IDE工具)也可以通过使用实现; XML 中的自定义工具命名空间(这真的不是很明显,但它是几个地方,我发现在那里将接受额外的数据之一):

If you want explicit xml markers in your generated code (i.e. [XmlType(...)], etc), simply use the p:xml command-line option, which (if you are using the IDE tools) can also be achieved by using ;xml in the "Custom Tool Namespace" (this really isn't obvious, but it is one of the few places I found where it would accept extra data):

基本上,任何进入自定义工具命名空间假设(由protobuf网)是一个分号列表开始与所需的命名空间,后跟发电机选项;因此,; XML 使用默认的名称空间的,然后添加XML选项,一样做号码:XML 在命令行上。

Basically, anything entered on the "Custom Tool Namespace" is assumed (by protobuf-net) to be a semicolon list starting with the desired namespace, followed by options for the generator; hence ;xml uses the default namespace, then adds the "xml" option, the same as doing p:xml on the command line.

这篇关于序列化协议缓存文件转换成XML /文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆