473,396 Members | 1,748 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,396 software developers and data experts.

XML serialization

Hello there,

I need to design classes, that can be serialized to XML like this:

<?xml version="1.0"?>
<Groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Group>
<Item name="Group 1 item 1" />
<Item name="Group 1 item 2" />
</Group>
<Group>
<Item name="Group 2 item 1" />
<Item name="Group 2 item 2" />
<Item name="Group 2 item 3" />
</Group>
<Group>
<Item name="Group 3 item 1" />
</Group>
</Groups>

Each node should be represented by C# class, and I have a problem
because:

[Serializable]
public class Groups : List<Group>
{
}

[Serializable]
public class Group : List<Item>
{
}

[Serializable]
public class Item
{
private string name;

[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}

gives:

<?xml version="1.0"?>
<ArrayOfArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfItem>
<Item name="Group 1 item 1" />
<Item name="Group 1 item 2" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 2 item 1" />
<Item name="Group 2 item 2" />
<Item name="Group 2 item 3" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 3 item 1" />
</ArrayOfItem>
</ArrayOfArrayOfItem>

I was able to get rid of ArrayOfArrayOfItem with this:

[Serializable]
[XmlRoot (ElementName = "Groups")]
public class Groups : List<Group>
{
}

[Serializable]
public class Group : List<Item>
{
}

[Serializable]
public class Item
{
private string name;

[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}

which gives:

<?xml version="1.0"?>
<Groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfItem>
<Item name="Group 1 item 1" />
<Item name="Group 1 item 2" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 2 item 1" />
<Item name="Group 2 item 2" />
<Item name="Group 2 item 3" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 3 item 1" />
</ArrayOfItem>
</Groups>

but I cannot change then name of inner "ArrayOfItem". The problem is
mainly, that I don't want to introduce to Groups class any property
member for Group collection - the name for such property will be
strange (either the main class is named Groups and the property
somehow else or the main class name is Foo and the property is
Groups). I know that the goal (from XML point of view) can be achieved
with something like below, but I don't like this 'Elements':

[Serializable]
public class Groups
{
private List<Groupelements = new List<Group();

[XmlElement (ElementName = "Group")]
public List<GroupElements
{
get
{
return this.elements;
}
}
}

[Serializable]
public class Group
{
private List<Itemelements = new List<Item();

[XmlElement (ElementName = "Item")]
public List<ItemElements
{
get
{
return this.elements;
}
}
}

[Serializable]
public class Item
{
private string name;

[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}

Thank you & best regards
Nov 2 '08 #1
2 1588
RafaƂ Grzybowski wrote:
but I cannot change then name of inner "ArrayOfItem". The problem is
mainly, that I don't want to introduce to Groups class any property
member for Group collection - the name for such property will be
strange (either the main class is named Groups and the property
somehow else or the main class name is Foo and the property is
Groups). I know that the goal (from XML point of view) can be achieved
with something like below, but I don't like this 'Elements':

[Serializable]
public class Groups
{
private List<Groupelements = new List<Group();

[XmlElement (ElementName = "Group")]
public List<GroupElements
{
get
{
return this.elements;
}
}
}

[Serializable]
public class Group
{
private List<Itemelements = new List<Item();

[XmlElement (ElementName = "Item")]
public List<ItemElements
{
get
{
return this.elements;
}
}
}

[Serializable]
public class Item
{
private string name;

[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
Im not sure there is another solution than the one you don't like.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 3 '08 #2
On Nov 2, 4:20*pm, Rafał Grzybowski <aguyngue...@gmail.comwrote:
Hello there,

I need to design classes, that can be serialized to XML like this:

<?xml version="1.0"?>
<Groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
* <Group>
* * <Item name="Group 1 item 1" />
* * <Item name="Group 1 item 2" />
* </Group>
* <Group>
* * <Item name="Group 2 item 1" />
* * <Item name="Group 2 item 2" />
* * <Item name="Group 2 item 3" />
* </Group>
* <Group>
* * <Item name="Group 3 item 1" />
* </Group>
</Groups>

Each node should be represented by C# class, and I have a problem
because:

* * [Serializable]
* * public class Groups : List<Group>
* * {
* * }

* * [Serializable]
* * public class Group : List<Item>
* * {
* * }

* * [Serializable]
* * public class Item
* * {
* * * * private string name;

* * * * [XmlAttribute (AttributeName="name")]
* * * * public string Name
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return this.name;
* * * * * * }
* * * * * * set
* * * * * * {
* * * * * * * * this.name = value;
* * * * * * }
* * * * }
* * }

gives:

<?xml version="1.0"?>
<ArrayOfArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
* <ArrayOfItem>
* * <Item name="Group 1 item 1" />
* * <Item name="Group 1 item 2" />
* </ArrayOfItem>
* <ArrayOfItem>
* * <Item name="Group 2 item 1" />
* * <Item name="Group 2 item 2" />
* * <Item name="Group 2 item 3" />
* </ArrayOfItem>
* <ArrayOfItem>
* * <Item name="Group 3 item 1" />
* </ArrayOfItem>
</ArrayOfArrayOfItem>

I was able to get rid of ArrayOfArrayOfItem with this:

* * [Serializable]
* * [XmlRoot (ElementName = "Groups")]
* * public class Groups : List<Group>
* * {
* * }

* * [Serializable]
* * public class Group : List<Item>
* * {
* * }

* * [Serializable]
* * public class Item
* * {
* * * * private string name;

* * * * [XmlAttribute (AttributeName="name")]
* * * * public string Name
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return this.name;
* * * * * * }
* * * * * * set
* * * * * * {
* * * * * * * * this.name = value;
* * * * * * }
* * * * }
* * }

which gives:

<?xml version="1.0"?>
<Groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
* <ArrayOfItem>
* * <Item name="Group 1 item 1" />
* * <Item name="Group 1 item 2" />
* </ArrayOfItem>
* <ArrayOfItem>
* * <Item name="Group 2 item 1" />
* * <Item name="Group 2 item 2" />
* * <Item name="Group 2 item 3" />
* </ArrayOfItem>
* <ArrayOfItem>
* * <Item name="Group 3 item 1" />
* </ArrayOfItem>
</Groups>

but I cannot change then name of inner "ArrayOfItem". The problem is
mainly, that I don't want to introduce to Groups class any property
member for Group collection - the name for such property will be
strange (either the main class is named Groups and the property
somehow else or the main class name is Foo and the property is
Groups). I know that the goal (from XML point of view) can be achieved
with something like below, but I don't like this 'Elements':

* * [Serializable]
* * public class Groups
* * {
* * * * private List<Groupelements = new List<Group();

* * * * [XmlElement (ElementName = "Group")]
* * * * public List<GroupElements
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return this.elements;
* * * * * * }
* * * * }
* * }

* * [Serializable]
* * public class Group
* * {
* * * * private List<Itemelements = new List<Item();

* * * * [XmlElement (ElementName = "Item")]
* * * * public List<ItemElements
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return this.elements;
* * * * * * }
* * * * }
* * }

* * [Serializable]
* * public class Item
* * {
* * * * private string name;

* * * * [XmlAttribute (AttributeName="name")]
* * * * public string Name
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * return this.name;
* * * * * * }
* * * * * * set
* * * * * * {
* * * * * * * * this.name = value;
* * * * * * }
* * * * }
* * }

Thank you & best regards
I'm curious: Why did you decide to use serialization? If you write
direct XML code (with System.Xml), you would have full control over
the file format, and your core classes (Groups, Group, and Item in
your example) would not be cluttered with serialization "stuff", like
SerializableAttribute and XmlAttributeAttribute. Or, do you prefer
having what I call "clutter" defined as part of each class requiring
persistence?

Doing some general research into the advantages of using
Runtime.Serialization. Is there some requirement/benefit that is not
possible (or very difficult) without it?
Nov 14 '08 #3

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.