I have an array with elements and want to have an iteration in the names of the elements when using Serialization in C#.
Let's say I have the class:
class MyClass {
public int[] MyArray { get; set; }
}
Currently the xml build with Serialization looks like this:
<MyClass>
<MyArray>
<int>1</int>
<int>2</int>
...
</MyArray>
</MyClass>
But I need it to look like this:
<MyClass>
<MyArray>
<int1>1</int1>
<int2>2</int2>
...
</MyArray>
</MyClass>
I want to communicate with another system which don't allow two elements with the same XML-path, so the default Serialization isn't possible.
I know I can use Annotations like [XmlArrayItem("int")]
, but I don't think it's possible to name the elements of the array different.
Worst case I have to implement the IXmlSerializable interface, but that would be a lot of work, because basically then I have to write the complete Serialization by myself with reflection.
So if anyone has an idea how to iterate the names of array elements with Annotation or any other hack, I would be very happy!
Solution: I used the solution from the similar question How do you deserialize XML with dynamic element names?. I changed it a bit for my problem, but now it's working! Thanks for the comments and tips!