序列化紧凑 XML
我有一个包含四个字段(DateTime、Enum、string、string)的类。我想以紧凑的方式将其序列化为一个 XML 元素或一系列 XML 元素。例如,我可能将其序列化为如下所示:
<i t='234233' a='3'><u>Username1</u><s1>This is a string</s1></i>
<i t='234233' a='4'><u>Username2</u><s1>This is a string</s1></i>
<i t='223411' a='1'><u>Username3</u><s1>This is a string</s1></i>
其中“i”是每个类实例,“t”是日期时间刻度,“a”是枚举值,元素是字符串。
我不想有根元素,但如果有的话,我希望它尽可能小。
我尝试将 XmlSerializer 与 XmlWriterSettings 类一起使用,但无法摆脱命名空间和根元素。
这样做的最佳方法是什么?我不是保存到文件,而是读取和写入内存中的字符串。
I have a class with four fields (DateTime, Enum, string, string). I want to serialize it to and from an XML element or a series of XML elements in a compact manner. For example, I might serialize it to something like this:
<i t='234233' a='3'><u>Username1</u><s1>This is a string</s1></i>
<i t='234233' a='4'><u>Username2</u><s1>This is a string</s1></i>
<i t='223411' a='1'><u>Username3</u><s1>This is a string</s1></i>
Where 'i' is each class instance, 't' is the DateTime ticks, 'a' is the enum value, and the elements are strings.
I'd prefer to not have a root element, but if I do have it, I'd like it to be as small as possible.
I've tried using XmlSerializer with the XmlWriterSettings class but I can't get rid of the namespaces and root element.
What's the best way of doing this? I'm not saving to a file, I'm reading and writing to strings in memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
System.Xml.Linq
并读取
PS:
您无需将
xElem
转换为字符串即可在内存中使用该 xmlSystem.Xml.Linq
and to read
PS:
You don't need to convert
xElem
to string in order to use that xml in memory如果您的数据就是这么简单,您可以直接使用
XmlWriter
:运行此程序会产生以下输出(为了清楚起见,我添加了换行符;它们不在输出中):
如果您想读回信息,则需要该根元素。最方便的方法是使用 LINQ2XML:
If your data is that simple, you can use
XmlWriter
directly:Running this program produces the following output (I added line breaks for clarity; they are not in the output):
You need that root element if you would like to read the information back. The most expedient way would be using LINQ2XML:
我个人只使用
StringBuilder
。如果大小是您最关心的问题,请考虑
json
或yaml
而不是 XML。I'd just use a
StringBuilder
, personally.If size is your #1 concern, consider
json
oryaml
instead of XML.我相信,您需要实现自己的序列化器,这将是一个痛苦。或者您可以在序列化后手动删除不需要的内容,这可能会起作用。
如果您关心大小,则应该使用
BinaryFormatter
序列化为二进制。如果需要将其存储为字符串,则始终可以对其进行 base-64 编码。 (BinaryFormatter
的工作方式几乎与XmlSerializer
一样,只不过输出是原始二进制文件,而不是格式良好的 XML。)You'll need to implement your own serializer, I believe, which will be a pain. That, or you could manually strip out what you don't need after serializing, which could work.
If size is your concern, you should be serializing to binary using
BinaryFormatter
. You can always base-64 encode it if you need to store it as a string. (BinaryFormatter
works almost just likeXmlSerializer
, except the output is raw binary, rather than nicely formatted XML.)