472,102 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

XMLSerializer problem, class inside class

Hi,

I'm having a problem with XMLSerializer, I have classes like the
following:

[Serializable]
public abstract class EulerObject
{
private Rectangle rect;
private string guid = "";

public virtual Rectangle Rect
{
get { return rect; }
set{ rect = value; }
}

public string GUID
{
get { return guid; }
set { guid = value; }
}

public abstract void Draw(Graphics g);

public abstract EulerObject MakeCopy(bool sameGuid);
}

[Serializable]
public abstract class EulerShape : EulerObject
{
private GraphicsPath graphPath;
private EulerLabel label = null;

public override Rectangle Rect
{
// do stuff
}

public abstract EulerShapes.ShapeType ShapeType
{
get;// { return shapeType; }
}

[XmlIgnore]
[SoapIgnore]
public GraphicsPath GraphPath
{
// stuff
}

public EulerLabel Label
{
get { return label; }
set { label = value; }
}

public EulerShape() { }
public abstract GraphicsPath CreateGraphicsPath(Rectangle
rect);
}

[Serializable]
public class EulerShapeElipse : EulerShape
{
public override EulerShapes.ShapeType ShapeType
{
get { return EulerShapes.ShapeType.Elipse; }
}

public EulerShapeElipse() : base() { }

public EulerShapeElipse(Rectangle theRect) : base(theRect) { }

public EulerShapeElipse(String uid, Rectangle theRect) :
base(uid, theRect) { }

public override GraphicsPath CreateGraphicsPath(Rectangle
rect)
{
GraphicsPath tempPath = new GraphicsPath();

tempPath.AddArc(rect, 0, 360);

return tempPath;
}

public override void Draw(Graphics g)
{
g.DrawPath(Pens.Black, this.GraphPath);

if (Label != null)
Label.Draw(g);
}

public override EulerObject MakeCopy(bool keepGuid)
{
EulerShapeElipse tempShape = new
EulerShapeElipse(this.Rect);

if (keepGuid == true)
tempShape.GUID = this.GUID;

return tempShape;
}
}

[Serializable]
public class EulerLabel : EulerObject
{
private string label;
private Font fontUsed;

public XmlFont Font
{
get { return new XmlFont(fontUsed); }
set { fontUsed = value.ToFont(); }
}

[XmlIgnore]
public Font FontUsed
{
get { return fontUsed; }
set { fontUsed = value; }
}

public string Label
{
get { return label; }
set { label = value; }
}

public EulerLabel()
{
this.fontUsed = SystemFonts.DefaultFont;
}

public override void Draw(Graphics g)
{
System.Windows.Forms.TextRenderer.DrawText(g, label,
fontUsed, this.Rect, Color.Black);
}

public override EulerObject MakeCopy(bool keepGuid)
{
EulerLabel tempLabel = new EulerLabel(this.Rect,
this.Label, this.FontUsed);

if (keepGuid == true)
tempLabel.GUID = this.GUID;

return tempLabel;
}
}

//http://www.codeproject.com/soap/xmlsettings.asp
[Serializable]
public struct XmlFont
{
public string FontFamily;
public GraphicsUnit GraphicsUnit;
public float Size;
public FontStyle Style;

public XmlFont(Font f)
{
FontFamily = f.FontFamily.Name;
GraphicsUnit = f.Unit;
Size = f.Size;
Style = f.Style;
}

public Font ToFont()
{
return new Font(FontFamily, Size, Style,
GraphicsUnit);
}
}

I have a few more classes that inherit from EulerShape.

I have a List<EulerShapethat contains various shapes that inherit
from EulerShape.

When using XMLSerializer it wont serialize the Label property inside
the classes based on EulerShape, yet it will serialize the EulerLabel
class itself if its not inside another class.

I've tried telling it to serialize the property by putting
[XmlAttribute(typeof(EulerLabel))] above it, but this then causes the
program to crash when trying to serialize it.

If anyone has a solution I'd apprechiate it, I have to try and solve
this problem ASAP so I can continue with a study for my dissertation
involving this application.

Regards,
Kris Wragg
BSc Software Engineering, 4th Year
Sheffield Hallam University

Mar 5 '07 #1
0 2700

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Harris Boyce | last post: by
2 posts views Thread by Chris Aitchison | last post: by
4 posts views Thread by Andy Neilson | last post: by
3 posts views Thread by Loui Mercieca | last post: by
1 post views Thread by Lloyd Dupont | last post: by
reply views Thread by Kristopher Wragg | last post: by
reply views Thread by leo001 | last post: by

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.