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"
I want to be able to override the names of the parent and child
elements (i.e. from say 'ArrayOfGuid' -> 'MyGUIDs' and 'guid' ->

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

Dim serializer As New XmlSerializer(arrGuids.GetType, Nothing, New
System.Type() {GetType(Guid)}, New XmlRootAttribute("MyGUIDs"),
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"),
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",
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

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


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"

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'

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.