473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"?>
<ArrayOfArrayOf Item 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>
</ArrayOfArrayOfI tem>

I was able to get rid of ArrayOfArrayOfI tem 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 "ArrayOfIte m". 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<Groupeleme nts = new List<Group();

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

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

[XmlElement (ElementName = "Item")]
public List<ItemElemen ts
{
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 1617
Rafał Grzybowski wrote:
but I cannot change then name of inner "ArrayOfIte m". 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<Groupeleme nts = new List<Group();

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

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

[XmlElement (ElementName = "Item")]
public List<ItemElemen ts
{
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...@gm ail.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"?>
<ArrayOfArrayOf Item 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>
</ArrayOfArrayOfI tem>

I was able to get rid of ArrayOfArrayOfI tem 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 "ArrayOfIte m". 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<Groupeleme nts = new List<Group();

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

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

* * * * [XmlElement (ElementName = "Item")]
* * * * public List<ItemElemen ts
* * * * {
* * * * * * 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
SerializableAtt ribute and XmlAttributeAtt ribute. 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.Seriali zation. 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
5023
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
1
4365
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 done this before but this time I have no idea why I am getting the error. Any ideas ?? THANKS ! <qcsttatus> <manual sourcerootfolder="" manualname="">
3
3178
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. It seems that either I can use default serialization or implement ISerializable. Is there any way to do both (e.g. extend the default serialization). In other words, I want to be able to implement my custom serialization code but call the...
6
2718
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 rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
3
2460
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 System.Collections.Comparer has 1 members, number of members deserialized is 0. at System.Runtime.Serialization.Formatters.Binary.ReadObjectInfo.GetMemberTypes(String
4
11417
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 look at innerException it says: "Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite'. OK it is problem to serialize all data so I'll implement my Serialization. I implemented ISerializable...
5
6118
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: Value -8590321990885400808 is outside the valid range . Parameter name: ticks at System.DateTime..ctor (Int64 ticks) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadPrimitiv
0
6616
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 object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream. XML serialization serializes only the public fields and property values of an object...
1
11109
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
5568
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 I am trying to read from a binary archive using Boost serialization. I am new to this, and it is possible that I have not understood the usage. In the code below, the string "faultblock" seems to be causing the problem. The code crashes in the ia...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.