Serializing int type without quotes

I have the following class for serialization:

[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; }
}

      

and it will be serialized and looks like this

<LabelRectangle X="15" Y="70" Width="10" Height="1" />

      

but I would like to get the following result:

<LabelRectangle X=15 Y=70 Width=10 Height=1 />

      

which serializes values ​​of type int without quotes. Is it possible and how, if so?

+3


source to share


4 answers


it wo n't be well-formed XML anymore - you defined the attribute

[XmlAttribute]

      



Attribute values ​​are always quoted!

+6


source


You shouldn't be doing this. Attribute values ​​must always be specified. You can use single or double quotes. So this is correct:

 <LabelRectangle X="15" Y="70" Width="10" Height="1" />

      

Is not:



 <LabelRectangle X=15 Y=70 Width=10 Height=1 />

      

See here .

Why do you want to deviate from the rules? Never had a good idea.

+3


source


XML attributes are not type-aware and their values ​​are always specified. Therefore, it is on purpose.

See also the XML specification :

AttValue  ::=  '"' ([^<&"] | Reference)* '"'
               |  "'" ([^<&'] | Reference)* "'"

      

So, all attribute values ​​are either entered in double quotes "

or single quotes '

.

+2


source


You want to make it an element as attributes are always quoted.

[XmlElement(DataType = "int",
ElementName = "Height")]
public int Height { get; set; }

      

0


source







All Articles