Array Serialization

S

S

Hi there,
Here's a question for ya:
I'm serializing something that looks like this:
[XmlRoot(ElementName="Widget")]
Class Widget --> My base class
[XmlArray(ElementName="TextGroups")]
[XmlArrayItem(ElementName="TextGroup",Type=typeof(ClassTextGroup))]
public ArrayList TextGroups --> An arraylist of collections
(TextGroup Collections)
TextGroup --> A custom collection
(collectionbase) of Text objects
[XmlRoot(ElementName="Text")]
Text --> The text object

Problem is, the xml comes out like this:
<Widget>
<TextGroups>
<ArrayofText>
<Text>

I would have thought my XmlArrayItem attribute on the ArrayList would have
taken care of this "Arrayof..." business, but is it running into a problem
because it's an array of, essentially, arrays? Any ideas on how I could fix
this without changing the structure of my class??

Thanks,
S
 
S

Steven Cheng[MSFT]

Hi Stephen,

From your description, you're doing some Xml Serialization work and you
want to serialize a certain class which contains a member and the member is
a Collection of another class's instance. And now you found the serialze
output xml will contain <Arrayof...> which wrap the collection members, and
you want to get rid of it?

Please feel free to let me know, if there is anything I misunderstand.

I'm not very sure about your detailed class code and the actual Xml Output
format you want, would you please provide me a clear class's code list and
also the complete xml output format?

Based on my test locally, here are my findings:


[XmlRoot("MyOrder1")]
public class Order1
{
[XmlArray(ElementName="MyItems")]
[XmlArrayItem(ElementName="MyItem",Type=typeof(Item))]
public Item [] ItemsOrders;
}

[XmlRoot("MyOrder2")]
public class Order2
{
[XmlArray(ElementName="MyItems")]
[XmlArrayItem(ElementName="MyItem",Type=typeof(Item))]
public ArrayList ItemsOrders;
}

public class Item
{
public string ItemID;
public string ItemName;
}

The Order1 and Order2 class are all has a member which is the collection of
Item class's instance, one is using a normal Array, and another use
ArrayList, When I serilize them, they out put the following xml:

###for Order1 class:
<?xml version="1.0" encoding="utf-8"?>
<MyOrder1 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyItems>
<MyItem>
<ItemID>0</ItemID>
<ItemName>Name0</ItemName>
</MyItem>
............................
<MyItem>
<ItemID>4</ItemID>
<ItemName>Name4</ItemName>
</MyItem>
</MyItems>
</MyOrder1>

###for Order2 class:
<?xml version="1.0" encoding="utf-8"?>
<MyOrder2 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyItems>
<MyItem>
<ItemID>0</ItemID>
<ItemName>Name0</ItemName>
</MyItem>
.................
<MyItem>
<ItemID>4</ItemID>
<ItemName>Name4</ItemName>
</MyItem>
</MyItems>
</MyOrder2>

They both output as the same style, without the "Arrayof..." element. And
from the MSDN code example, the "Arrayof .." will occur when serializing a
CustomCollection class implement the ICollection interface. Here are the
related docs in MSDN:

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

#http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingserial
izationbyxmlserializerwithattributes.asp?frame=true
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconanexampleofxmlseri
alizationwithxmlserializer.asp?frame=true

Please feel free to let me know if you have any questions. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

Hi Stephen,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

S

Thanks for the reply Steven.

I think I'm okay.. I changed the structure to where it would work..


Steven Cheng said:
Hi Stephen,

From your description, you're doing some Xml Serialization work and you
want to serialize a certain class which contains a member and the member is
a Collection of another class's instance. And now you found the serialze
output xml will contain <Arrayof...> which wrap the collection members, and
you want to get rid of it?

Please feel free to let me know, if there is anything I misunderstand.

I'm not very sure about your detailed class code and the actual Xml Output
format you want, would you please provide me a clear class's code list and
also the complete xml output format?

Based on my test locally, here are my findings:


[XmlRoot("MyOrder1")]
public class Order1
{
[XmlArray(ElementName="MyItems")]
[XmlArrayItem(ElementName="MyItem",Type=typeof(Item))]
public Item [] ItemsOrders;
}

[XmlRoot("MyOrder2")]
public class Order2
{
[XmlArray(ElementName="MyItems")]
[XmlArrayItem(ElementName="MyItem",Type=typeof(Item))]
public ArrayList ItemsOrders;
}

public class Item
{
public string ItemID;
public string ItemName;
}

The Order1 and Order2 class are all has a member which is the collection of
Item class's instance, one is using a normal Array, and another use
ArrayList, When I serilize them, they out put the following xml:

###for Order1 class:
<?xml version="1.0" encoding="utf-8"?>
<MyOrder1 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyItems>
<MyItem>
<ItemID>0</ItemID>
<ItemName>Name0</ItemName>
</MyItem>
............................
<MyItem>
<ItemID>4</ItemID>
<ItemName>Name4</ItemName>
</MyItem>
</MyItems>
</MyOrder1>

###for Order2 class:
<?xml version="1.0" encoding="utf-8"?>
<MyOrder2 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyItems>
<MyItem>
<ItemID>0</ItemID>
<ItemName>Name0</ItemName>
</MyItem>
.................
<MyItem>
<ItemID>4</ItemID>
<ItemName>Name4</ItemName>
</MyItem>
</MyItems>
</MyOrder2>

They both output as the same style, without the "Arrayof..." element. And
from the MSDN code example, the "Arrayof .." will occur when serializing a
CustomCollection class implement the ICollection interface. Here are the
related docs in MSDN:

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingseriali
zationbyxmlserializerwithattributes.asp?frame=true
#http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingserial
izationbyxmlserializerwithattributes.asp?frame=true
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconanexampleofxmlseri
alizationwithxmlserializer.asp?frame=true

Please feel free to let me know if you have any questions. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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