Summary
When using the XmlSerializer
class, serializing a List<T>
(where T can be serialized with XmlSerializer
without problems) using XmlAttributeOverrides
such as this:
using xmls = System.Xml.Serialization;
...
xmls.XmlAttributeOverrides attributeOverrides = new xmls.XmlAttributeOverrides();
attributeOverrides.Add(typeof(T), new xmls.XmlAttributes()
{
XmlRoot = new xmls.XmlRootAttribute("foo")
});
attributeOverrides.Add(typeof(List<T>), new xmls.XmlAttributes()
{
XmlArray = new xmls.XmlArrayAttribute("foobar"),
XmlArrayItems = { new xmls.XmlArrayItemAttribute("foo") },
});
will throw the following InvalidOperationExcpetion at the inner-most exception:
System.InvalidOperationException: XmlRoot and XmlType attributes may not be specified for the type System.Collections.Generic.List`1[[T, programname, Version=versionnumber, Culture=neutral, PublicKeyToken=null]].
What I expect from the serializer
<texparams>
<texparam pname="TextureMinFilter" value="9729"/>
<texparam pname="TextureMagFilter" value="9729"/>
</texparams>
What I can sucessfully get at the moment
<ArrayOfTextureParameter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TextureParameter pname="TextureMinFilter" value="9729" />
<TextureParameter pname="TextureMagFilter" value="9728" />
</ArrayOfTextureParameter>
Background Info
I have been messing around with XML Serialization lately but have come across a problem. I am trying to serialize and deserialize some classes that wrap OpenGL textures.
In one of my classes (which I appropriately called BitmapTexture2d
) I have a Bitmap
field, which I wish to store in a Base64 element like so:
<bitmap64>
*base64goeshere*
</bitmap64>
Since I want to keep my code as neat as possible I decided to use the IXmlSerializable
interface instead of creating a property which can convert a string
and a Bitmap
back and forth.
Later on in the process I decided to use the XmlSerializer
class to generate the XML for a single field defined in Texture2d
(which BitmapTexture2d
is derived from) called Parameters
(which is a List<TextureParameter>
and TextureParameter
is serializable by the XmlSerialization
class). However this is how the serializer defaulted to serializing the List<TextureParameter>
:
<ArrayOfTextureParameter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TextureParameter pname="TextureMinFilter" value="9729" />
<TextureParameter pname="TextureMagFilter" value="9728" />
</ArrayOfTextureParameter>
After seeing this, I decided to try and change the names of the nodes. After some research (where I landed at stackoverflow multiple times) I discovered the XmlAttributeOverrides
class which can be passed to the constructor of XmlSerializer
to add/override node names etc.
After writing out my code, the constructor for the sub-serializer started throwing an exception as described above. I have tried using an array which threw the same exception. I later though to serialize each element in the list one by one, but came to the conclusion that it was harder than I thought to achieve it that way. I posted this question somewhere else with no answer. And here I am...
See Question&Answers more detail:
os