473,770 Members | 6,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Xml serialization attribute for nested List

Is it possible by using attributes to serialize a <List<List<Poin t>>
property to output as shown below?

Current Output

<PolyPolyline >
<ArrayOfPoint >
<Point>
<X>5</X>
<Y>5</Y>
</Point>
<Point>
<X>10</X>
<Y>15</Y>
</Point>
Preferred Output

<PolyPolyline >
<Polyline>
<Point X=5 Y=5 />
<Point X=10 Y=15 />
Here is some sample code of the class:

--------------------------------------------------------------

using System.Drawing;
using System.Collecti ons.Generic;
using System.Xml;
using System.Xml.Seri alization;

namespace Cjf
{
public class Test
{
private List<List<Point >_polyPolylin e = new
List<List<Point >>();

public List<List<Point >PolyPolyline
{
get { return _polyPolyline; }
set { _polyPolyline = value; }
}

public static void Serialize(strin g filename, Test test)
{
XmlSerializer serializer = new XmlSerializer(t ypeof(Test));

XmlWriter writer = XmlWriter.Creat e(filename);
serializer.Seri alize(writer, test);
writer.Close();
}

public static Test Deserialize(str ing filename)
{
XmlSerializer s = new XmlSerializer(t ypeof(Test));

XmlReader reader = XmlReader.Creat e(filename);
Test test = (Test)s.Deseria lize(reader);
reader.Close();

return test;
}

public static Test CreateTestData( )
{
Test test = new Test();

List<Pointpolyl ine = new List<Point>();
test.PolyPolyli ne.Add(polyline );
polyline.Add(ne w Point(5, 5));
polyline.Add(ne w Point(10, 15));
polyline.Add(ne w Point(20, 15));
polyline.Add(ne w Point(25, 30));

polyline = new List<Point>();
test.PolyPolyli ne.Add(polyline );
polyline.Add(ne w Point(-5, -5));
polyline.Add(ne w Point(-10, -15));
polyline.Add(ne w Point(-20, -15));

return test;
}
}
}
-------------------------------------------------------------

string path = System.IO.Path. GetTempPath();

Cjf.Test test = Cjf.Test.Create TestData();
Cjf.Test.Serial ize(path + "test1.xml" , test);

Oct 5 '06 #1
1 7013
Why not just break your code out into multiple serializable classes and use
properties to reference the child elements/attributes?

[XmlRoot{"PolyPo lyline")]
public class PolyPolyline
{
private List<ArrayOfPoi ntmlArrayOfPoin t = new List<ArrayOfPoi nt>();

[XmlElement("Arr ayOfPoint")]
public List<ArrayOfPoi ntArrayOfPoint
{
get mlArrayOfPoint ;
set mlArrayOfPoint;
}
}

public class ArrayOfPoint
{
private Point mpPoint;

[XmlElement("Poi nt")]
public Point Point
{
get mpPoint;
set mpPoint;
}
}

public class Point
{
private int miX;
private int miY;

[XmlAttribute("X ")]
public int X
{
get miX;
set miX;
}
[XmlAttribute("X ")]
public int Y
{
get miY;
set miY;
}
}
<ch*******@gmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Is it possible by using attributes to serialize a <List<List<Poin t>>
property to output as shown below?

Current Output

<PolyPolyline >
<ArrayOfPoint >
<Point>
<X>5</X>
<Y>5</Y>
</Point>
<Point>
<X>10</X>
<Y>15</Y>
</Point>
Preferred Output

<PolyPolyline >
<Polyline>
<Point X=5 Y=5 />
<Point X=10 Y=15 />
Here is some sample code of the class:

--------------------------------------------------------------

using System.Drawing;
using System.Collecti ons.Generic;
using System.Xml;
using System.Xml.Seri alization;

namespace Cjf
{
public class Test
{
private List<List<Point >_polyPolylin e = new
List<List<Point >>();

public List<List<Point >PolyPolyline
{
get { return _polyPolyline; }
set { _polyPolyline = value; }
}

public static void Serialize(strin g filename, Test test)
{
XmlSerializer serializer = new XmlSerializer(t ypeof(Test));

XmlWriter writer = XmlWriter.Creat e(filename);
serializer.Seri alize(writer, test);
writer.Close();
}

public static Test Deserialize(str ing filename)
{
XmlSerializer s = new XmlSerializer(t ypeof(Test));

XmlReader reader = XmlReader.Creat e(filename);
Test test = (Test)s.Deseria lize(reader);
reader.Close();

return test;
}

public static Test CreateTestData( )
{
Test test = new Test();

List<Pointpolyl ine = new List<Point>();
test.PolyPolyli ne.Add(polyline );
polyline.Add(ne w Point(5, 5));
polyline.Add(ne w Point(10, 15));
polyline.Add(ne w Point(20, 15));
polyline.Add(ne w Point(25, 30));

polyline = new List<Point>();
test.PolyPolyli ne.Add(polyline );
polyline.Add(ne w Point(-5, -5));
polyline.Add(ne w Point(-10, -15));
polyline.Add(ne w Point(-20, -15));

return test;
}
}
}
-------------------------------------------------------------

string path = System.IO.Path. GetTempPath();

Cjf.Test test = Cjf.Test.Create TestData();
Cjf.Test.Serial ize(path + "test1.xml" , test);

Oct 17 '06 #2

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

Similar topics

8
6012
by: Joe | last post by:
I have several classes which I need serialized. Here is their class definitions: public class TopContainer { private LevelTwoType m_levelTwo; public LevelTwoType LevelTwo { get {
5
7054
by: Rene | last post by:
I created a class (SomeClass), on that class, I declared a delegate (OnXyz) and then declared an event based on that delegate (Xyz). All this in the same class. After that, I created another class where I instantiate my previous class (SomeClass) and attach to its event like this: someObject. Xyz += new SomeClass. OnXyz (PrivateFunction); Life is good when I serialize the object but things go to hell when I deserialize it. What a hell?...
8
2306
by: Pavils Jurjans | last post by:
Hello, I have been developing an Ajax-style framework for couple of years now. Now I am reworking some parts of it. The problem was that I used to use JSON for JavaScript value serialization/deserialization, but this approach has some limitations when it comes to large data - the JavaScript at the client side has too much load working with it. So, I thought, I'd rework the serialization to XML format. The idea is short-as-possible...
3
15397
by: Ympostor | last post by:
Hello. I am using attribute for redefining the element name used in XML Serialization, but it only works for the parent elemnt: Simple test code (referencing System.Xml): using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization;
0
3395
by: Eivind Gussiås Løkseth | last post by:
I've been reading Keith Pijanowski's article - Enrich Your XML Serialization With Schema Providers In The .NET Framework - on how to do custom serialization of objects. Link to the article: http://msdn.microsoft.com/msdnmag/issues/06/06/ClassToContract/default.aspx Now I'm trying to do the same ting in a more complex scenario, with two nested classes. The nested class is used in a collection property of the main class, and I want that...
0
1203
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in site but only basics serialization is there. Topics: 1) serialize a nested object in mutiple files by maintaining the relation on parent level. ie, let say object1 containts object2 and object2 contains object3 , while serializing the object1,...
8
1402
by: Casper | last post by:
Hi, i read several articles about serialization. I know now that it is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. Now i did some tests in order to understand it better: I first executed the code below (this (summarized) code produces a virtual simple shopping cart which is put in the Profile of the user) with...
2
1345
by: Bart | last post by:
Hi, i read several articles about serialization. I know now that it is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location and that the serializable tag before the class makes the class serializable. Now i did some tests in order to understand it better, based on a example i found in the Wrox book "beginning asp.net 2.0".
2
3846
by: Peter Duniho | last post by:
I've been learning about mechanisms .NET offers to export data. The initial goal is to see what sorts of ways are available to save an application's state (document, internal database, whatever). Not counting storing data in a database (which is obviously suitable for some things, but not necessary or even necessarily desirable for other things), here's what I've explored: Serializable attribute, with BinaryFormatter and SoapFormatter...
0
9425
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
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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
10001
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
8880
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...
0
6676
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
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.