Alexus
Alexus

Reputation: 296

XmlAttributeAttribute type in generated XML file

When I serialize this class:

public class Camera
{
    ------ // other informations
    private long cameraIdField;
    ------

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public long CameraId
    {
        get
        {
            return this.cameraIdField;
        }
        set
        {
            this.cameraIdField = value;
        }
    }
}

The XML file produced contains the value of cameraID between "" :

<CameraId>"0"<CameraId>

What I need is the int value like this :

<CameraId>0<CameraId>

How can I export the cameraId as an int ? Thanks for your help.

Upvotes: 0

Views: 282

Answers (1)

WagoL
WagoL

Reputation: 107

This is working:

Class:

public class Camera
{
    [XmlElement]
    public long CameraId { get; set; }
    [XmlIgnore]
    public string Xml { get { return Extension.ToXmlString<Camera>(this); } }
}

Xml:

<?xml version="1.0" encoding="utf-16"?>
<Camera xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CameraId>2</CameraId>
</Camera>

You had to use XML element instead of attribute.

Upvotes: 1

Related Questions