By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

Xml serialization attribute for nested List

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.