Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create your custom XmlAttribute Serialization

I'm looking on who to customize the serialization of an attribute. I thought it would be simple, but I was not able to achieve what I wanted to do the way I wanted.

So here is a simple example:

Class Definition:

Class MyClass
{
    [XmlAttribute("myAttribute")]
    public int[] MyProperty { get; set; }
}

Xml Result that I would like:

<MyClass myAttribute="1 2 3... N" />

The only work around I did to have that was to put a [XmlIgnore] attribute and create another property with some code that did the transformation.

So, my question, is there a better way than creating a new property? Maybe there is some kind of TypeConverter you can create so the serializer would use it?

Also, I've tried to use the Type attribute but without success. (Always getting exceptions). But from what I've read, it's for already defined datatype.

[XmlAttribute("myAttribute", typeof(MyConverter))]
public int[] MyProperty { get; set; }

Another interesting way would be like that:

[XmlAttribute("myAttribute")]
[XmlConverter(typeof(MyConverter))]
public int[] MyProperty { get; set; }

Thanks.


Edit Since no solution like I was looking for was presented, I finally decided to opt for the "IXmlSerializable" solution.

like image 568
Sauleil Avatar asked Nov 28 '10 21:11

Sauleil


People also ask

What is the correct way of using XML serialization?

XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.

What is XML serialization and Deserialization in C#?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.

Which of the following attribute controls XML serialization of the attribute target as an XML root element?

XmlRootAttribute Class (System.Xml.Serialization) Controls XML serialization of the attribute target as an XML root element.

What is Xmlserializer C#?

XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization re-creates the object in its original state from the XML output.


1 Answers

You can either:

  1. Implement IXmlSerializable and handle all the serialization/deserialization for your type manually
  2. Use a surrogate property:

    [XmlIgnore]
    public int[] MyProperty { get; set; }
    
    
    [XmlAttribute("myAttribute")]
    public string _MyProperty
    {
        get
        {
            return string.Join(" ", MyProperty.Select(x => x.ToString()).ToArray());
        }
        set
        {
            MyProperty = value.Split(' ').Select(x => int.Parse(x)).ToArray();
        }
    }
    
like image 133
Diego Mijelshon Avatar answered Oct 04 '22 14:10

Diego Mijelshon