序列化没有引号的int类型
我有以下类来序列化:
[Serializable] public class LabelRectangle { [XmlAttribute] public int X { get; set; } [XmlAttribute] public int Y { get; set; } [XmlAttribute] public int Width { get; set; } [XmlAttribute] public int Height { get; set; } }
它将被序列化,看起来像这样
但我想得到以下结果:
这是序列化没有引号的int类型值。 有可能,如果是的话?
这将不再是一个格式良好的XML – 您定义了属性
[XmlAttribute]
属性值总是在引号!!
你不应该这样做。 必须始终引用属性值。 可以使用单引号或双引号。 所以这是正确的:
这不是:
看到这里 。
你为什么要偏离规则? 永远不是一个好主意。
XML属性不知道类型,它们的值总是被引用。 所以这是故意的。
另请参阅XML规范 :
AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
因此,所有属性值都要加上双引号"
或带单引号'
。
您将希望将其设为元素,因为属性始终在引号中。
[XmlElement(DataType = "int", ElementName = "Height")] public int Height { get; set; }