XMLSerializing Array/ArrayList Using XMLAttributeOverrides

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Gregory

    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.Seri alization.XmlSe rializer(arrGui ds.GetType)
    Dim writer As New System.IO.Strin gWriter
    serializer.Seri alize(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>d79f6a3 3-8252-4c4e-83bd-db358bd682bd</guid>
    <guid>e7a8fdc 2-f555-45f6-9d09-2cff558924b0</guid>
    <guid>2646207 e-94b4-4d36-a739-913e9288eb9f</guid>
    <guid>fe9c1a5 a-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(a rrGuids.GetType , Nothing, New
    System.Type() {GetType(Guid)} , New XmlRootAttribut e("MyGUIDs"),
    Nothing)


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

    Dim serializer As New XmlSerializer(a rrGuids.GetType , attr_or, New
    System.Type() {GetType(Guid)} , New XmlRootAttribut e("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 XmlAttributeOve rrides
    Dim XMLAttrs As New XmlAttributes
    XMLAttrs.XmlArr ayItems.Add(New XmlArrayItemAtt ribute("MyGUID" ,
    GetType(Guid)))
    attr_or.Add(Get Type(Guid()), XMLAttrs)

    Gives 'System.Invalid OperationExcept ion: 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(Get Type(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 XMLSerializatio n about the
    place) I could *almost* achieve the desired result by creating and
    serializing an object based on the following class:

    Class MyObj

    <XmlArray(Eleme ntName:="MyGUID s", Namespace:="",
    Isnullable:=Fal se), XmlArrayItem(El ementName:="MyG UID",
    Type:=GetType(S ystem.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>6a2e12b c-4948-4874-aa7d-deda3440dd89</MyGUID>
    <MyGUID>1a896c8 1-f4b1-4bd4-b384-97fa8bb1fd19</MyGUID>
    <MyGUID>de8cf59 2-7351-4733-b914-d9c57e61addc</MyGUID>
    <MyGUID>838b8cc 6-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 XmlAttributeOve rrides class for this 'simple'
    scenario?

    Simon Gregory
Working...