473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 3151

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hollywood | last post by:
First, is it possible to use XmlArrayItem to declare two data types, DT1 and DT2 where DT2 is a derived class, in an ArrayList as the same ElementName? The following throws a...
1
by: Taco | last post by:
The code below will serialize the Catalog object something like: <Catalog> <CatalogTitle>MyCatalog</CatalogTitle> <Books> <Book> <Title>Book1</Title> </Book> <Book> <Title>Book2</Title>...
3
by: Kishore Gopalan | last post by:
Hi, I have the following class. Assembly: A.dll public class Customers { public Customer customer; } public class Customer { public string name;
9
by: Steve | last post by:
Hello, I created a structure ABC and an array of type ABC Public Structure ABC Dim str1 As String Dim int1 As Integer End Structure Public ABC1 As New ABC, ABC2 As New ABC
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
1
by: Joe | last post by:
I have a class that is being returned by several WebMethods. Ok - all is good. Now if I have this class implement IEnumerable, the data is no longer serialized. This class does contain a...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
0
by: harshal.borade | last post by:
I am using XmlSerializer with XmlAttributeOverrides. The XmlAttributeOverrides are used for properties which hold element/s derived from "AbstractMessage" type. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.