如何序列化一个整数值? [英] How to serialise an integer value?

查看:32
本文介绍了如何序列化一个整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我刚刚开始使用 XML 序列化.

As I'm just starting working with XML serialisation.

目前我有一个类,包含一个X"属性:

Currently I have a class, containing an "X" attribute:

public class Offset
{
    [XmlAttribute(AttributeName = "X")]
    public int X { get; set; }

XML 序列化生成如下内容:

XML serialisation generates something like:

<Offset ... X="0" .../>
              ^^^
               |
               +--- looks like a string

我希望 XML 序列化看起来像:

I would like the XML serialisation to look like:

<Offset ... X=0 ... />
              ^
              |
              +--- I'd like an integer.

为了完成这件事,这就是我所做的:

In order to get this done, this is what I do:

public class Offset
{
    [XmlAttribute(AttributeName = "X", DataType = "int")]
    public int X { get; set; }

但这也是我的序列化 XML 的样子:

But again this is what my serialised XML looks like:

<Offset ... X="0" .../>  <!-- the double quotes are still there -->

有两种可能:

  • 要么我做错了什么,要么有另一种方法来删除那些双引号.
  • 所需的 XML(不带双引号)无效.问题是:我使用了不同的 XML 验证器(来自 Notepad++ 的 XML 插件,说一切正常,另一个在线验证器,说正好相反),所以我不确定.

谁能告诉我所需的 XML 格式是否有效,如果是,那是什么?告诉 XML 序列化器属性必须作为整数处理的最简单方法(如果是浮点数,如何执行此操作)?

Can anyone tell me if the required XML format is valid and in case yes, what's the easiest way to tell the XML serialiser that an attribute must be handled as an integer (and in case of a floating point number, how to do this)?

提前致谢

推荐答案

已经生成的输出是正确的.您说您想要的输出:无效的 xml - 属性必须使用引号.这里没什么可做的——代码和输出已经很好了,值已经是一个表示为 xml 属性的整数.此意图在不太严格的上下文中有效,例如 html,但不适用于 xml.

The output that is already being generated is correct. The output you say you want: is not valid xml - attributes must use quotes. There is nothing to do here - the code and output is already fine, and the value is already an integer expressed as an xml attribute. This intent is valid in less strict contexts such as html, but not xml.

编辑关于有效 XML 的官方文档

这个链接提到属性必须被引用,所以<偏移量 ... X=0 .../> 不是有效的 XML.

This link mentions that attributes must be quoted, so <Offset ... X=0 ... /> is not valid XML.

这篇关于如何序列化一个整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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