XMLSerializing Array/ArrayList Using XMLAttributeOverrides

S

Simon Gregory

I'm trying to override the default elementnames in the (seemingly)
simple case of serializing an array of GUIDs into XML. Here's the
basic code I started with:


Dim arrGuids(3) as Guid

arrGuids(0) = Guid.NewGuid
arrGuids(1) = Guid.NewGuid
arrGuids(2) = Guid.NewGuid
arrGuids(3) = Guid.NewGuid

Dim serializer As New
System.Xml.Serialization.XmlSerializer(arrGuids.GetType)
Dim writer As New System.IO.StringWriter
serializer.Serialize(writer, arrGuids)
Return writer.ToString


This produces the following result as you would expect:

<ArrayOfGuid xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<guid>d79f6a33-8252-4c4e-83bd-db358bd682bd</guid>
<guid>e7a8fdc2-f555-45f6-9d09-2cff558924b0</guid>
<guid>2646207e-94b4-4d36-a739-913e9288eb9f</guid>
<guid>fe9c1a5a-b7b5-4645-909f-2786016e6f6d</guid>
</ArrayOfGuid>


I want to be able to override the names of the parent and child
elements (i.e. from say 'ArrayOfGuid' -> 'MyGUIDs' and 'guid' ->
'MyGUID')

I've managed to get part way there by successfully overriding the name
of the Root node, with a slight change to the XmlSerializer line as
follows:

Dim serializer As New XmlSerializer(arrGuids.GetType, Nothing, New
System.Type() {GetType(Guid)}, New XmlRootAttribute("MyGUIDs"),
Nothing)


The next step as far as I can tell is to specify an
XMLAttributeOverride object to override the names of the <guid>
elements, by providing the second parameter in the XmlSerializer line:

Dim serializer As New XmlSerializer(arrGuids.GetType, attr_or, New
System.Type() {GetType(Guid)}, New XmlRootAttribute("MyGuids"),
Nothing)


Unfortunately I cannot seem to find the correct syntax to achieve what
I want. Here an example of what I am trying:

Dim attr_or As New XmlAttributeOverrides
Dim XMLAttrs As New XmlAttributes
XMLAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("MyGUID",
GetType(Guid)))
attr_or.Add(GetType(Guid()), XMLAttrs)

Gives 'System.InvalidOperationException: XmlRoot and XmlType
attributes may not be specified for the type System.Guid().'


I have tried all sorts of combinations and different attributes in
addition to this, but still haven't got very far.

I suspect that I need to specify a member name in the final add
statement:

attr_or.Add(GetType(Guid()), "<member Name>", XMLAttrs)

But I haven't been able to find one that does the trick.


Also, if I replace the GUID() declaration (Dim arrGuids(3) as Guid
) to an ArrayList of GUIDs I also get stuck in the same place.


I know that (from all the examples of XMLSerialization about the
place) I could *almost* achieve the desired result by creating and
serializing an object based on the following class:

Class MyObj

<XmlArray(ElementName:="MyGUIDs", Namespace:="",
Isnullable:=False), XmlArrayItem(ElementName:="MyGUID",
Type:=GetType(System.Guid))> _
Public arrGuids(3) As Guid

End Class

But I don't want this approach, as:
a) I don't really want to have to create a specific class just to be
able to do this, as I need to do a similar thing in many different
guises/circumstances.

and

b) Additionally MyGuids will no longer be the root node, as I end up
with the extra element that comes from the parent class:

<MyObj xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyGUIDs>
<MyGUID>6a2e12bc-4948-4874-aa7d-deda3440dd89</MyGUID>
<MyGUID>1a896c81-f4b1-4bd4-b384-97fa8bb1fd19</MyGUID>
<MyGUID>de8cf592-7351-4733-b914-d9c57e61addc</MyGUID>
<MyGUID>838b8cc6-d42b-43a2-93d8-9f167daaa3b0</MyGUID>
</MyGUIDs>
</MyObj>

Which again, isn't what I want!

Is there anybody out there that can point me to the correct
syntax/setup of the XmlAttributeOverrides class for this 'simple'
scenario?

Simon Gregory
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top