469,603 Members | 2,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

XMLSerializing Array/ArrayList Using XMLAttributeOverrides

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.Ge tType)
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
Nov 20 '05 #1
0 2972

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hollywood | last post: by
9 posts views Thread by Steve | last post: by
4 posts views Thread by Peter | last post: by
18 posts views Thread by Sam | last post: by
5 posts views Thread by Paulers | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.