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

Xml serialization attribute for nested List

Is it possible by using attributes to serialize a <List<List<Point>>
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.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

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

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

public static void Serialize(string filename, Test test)
{
XmlSerializer serializer = new XmlSerializer(typeof(Test));

XmlWriter writer = XmlWriter.Create(filename);
serializer.Serialize(writer, test);
writer.Close();
}

public static Test Deserialize(string filename)
{
XmlSerializer s = new XmlSerializer(typeof(Test));

XmlReader reader = XmlReader.Create(filename);
Test test = (Test)s.Deserialize(reader);
reader.Close();

return test;
}

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

List<Pointpolyline = new List<Point>();
test.PolyPolyline.Add(polyline);
polyline.Add(new Point(5, 5));
polyline.Add(new Point(10, 15));
polyline.Add(new Point(20, 15));
polyline.Add(new Point(25, 30));

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

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

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

Cjf.Test test = Cjf.Test.CreateTestData();
Cjf.Test.Serialize(path + "test1.xml", test);

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

[XmlRoot{"PolyPolyline")]
public class PolyPolyline
{
private List<ArrayOfPointmlArrayOfPoint = new List<ArrayOfPoint>();

[XmlElement("ArrayOfPoint")]
public List<ArrayOfPointArrayOfPoint
{
get mlArrayOfPoint ;
set mlArrayOfPoint;
}
}

public class ArrayOfPoint
{
private Point mpPoint;

[XmlElement("Point")]
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*******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Is it possible by using attributes to serialize a <List<List<Point>>
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.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

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

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

public static void Serialize(string filename, Test test)
{
XmlSerializer serializer = new XmlSerializer(typeof(Test));

XmlWriter writer = XmlWriter.Create(filename);
serializer.Serialize(writer, test);
writer.Close();
}

public static Test Deserialize(string filename)
{
XmlSerializer s = new XmlSerializer(typeof(Test));

XmlReader reader = XmlReader.Create(filename);
Test test = (Test)s.Deserialize(reader);
reader.Close();

return test;
}

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

List<Pointpolyline = new List<Point>();
test.PolyPolyline.Add(polyline);
polyline.Add(new Point(5, 5));
polyline.Add(new Point(10, 15));
polyline.Add(new Point(20, 15));
polyline.Add(new Point(25, 30));

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

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

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

Cjf.Test test = Cjf.Test.CreateTestData();
Cjf.Test.Serialize(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
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
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...
8
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...
3
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...
0
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:...
0
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...
8
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...
2
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...
2
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). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.