Chris James

Chris James

  • NA
  • 4
  • 0

C# class referencing XML question

Nov 15 2005 8:54 AM
Hi

I have a class which essentially describes a simple language using XML. The Class in this case TsmlShape allows me to manipulate the document for example there is a method to get and set the colour attribute for the shape Example below. What I am trying to do (highlighted in red) is on the set function of the method update the corresponding XML element, this is where I am having the problem. Given that the element has a corresponding node in the xml, how should I go about this?

Any help is as always greatly appreciated. Also if I have not been very clear (I am quite new to C# and XML) then please do ask me to clarify what Im trying to do.

Cheers,

Chris




public class TsmlShape

{

public enum ShapeTypes

{

Rectangle=1,

Line=2,

Ellipse=3,

Pie=4

};

public enum GradientTypes

{

Horizontal=1,

Vertical=2,

ForwardDiagonal=3,

BackwardDiagonal=4,

None=5

};

private string sID = "";

private string sCoords = "";

private string sColor = "#ffffff";

private string sColor2 = "#ffffff";

private int iPenWidth = 1;

private int iStartAngle = 0;

private int iSweepAngle = 0;

private ShapeTypes iType = ShapeTypes.Rectangle;

private GradientTypes iGradient = GradientTypes.None;

XmlElement tmpXel;

XmlNode tmpNode;

//--------------------------------

public TsmlShape(string coords)

{

iType = ShapeTypes.Rectangle;

iGradient = GradientTypes.None;

sColor = "#ffffff";

sColor2 = "#ffffff";

iPenWidth = 1;

iStartAngle = 0;

iSweepAngle = 0;

sCoords = coords;

}

//--------------------------------

public TsmlShape(string coords, ShapeTypes type)

{

iType = type;

iGradient = GradientTypes.None;

sColor = "#ffffff";

sColor2 = "#ffffff";

iPenWidth = 1;

iStartAngle = 0;

iSweepAngle = 0;

sCoords = coords;

}

//--------------------------------

public TsmlShape(string coords, ShapeTypes type, string color)

{

iType = type;

iGradient = GradientTypes.None;

sColor = color;

sColor2 = "#ffffff";

iPenWidth = 1;

iStartAngle = 0;

iSweepAngle = 0;

sCoords = coords;

}

//--------------------------------

public TsmlShape(string coords, ShapeTypes type, string color, int penwidth)

{

iType = type;

iGradient = GradientTypes.None;

sColor = color;

sColor2 = "#ffffff";

iPenWidth = penwidth;

iStartAngle = 0;

iSweepAngle = 0;

sCoords = coords;

}

//--------------------------------

public TsmlShape(string coords, ShapeTypes type, string color, string color2, GradientTypes gradient)

{

iType = type;

iGradient = gradient;

sColor = color;

sColor2 = color2;

iPenWidth = 1;

iStartAngle = 0;

iSweepAngle = 0;

sCoords = coords;

}

//--------------------------------

public TsmlShape(string coords, ShapeTypes type, string color, string color2, GradientTypes gradient, int penwidth, int startangle, int sweepangle)

{

iType = type;

iGradient = gradient;

sColor = color;

sColor2 = color2;

iPenWidth = penwidth;

iStartAngle = startangle;

iSweepAngle = sweepangle;

sCoords = coords;

}

//--------------------------------

/// <summary>

/// Declare an explicit conversion from a TsmlShape to an TsmlElement:

/// </summary>

/// <param name="roman"></param>

/// <returns></returns>

static public implicit operator TsmlElement(TsmlShape shp)

{

TsmlElement el = new TsmlElement(shp.TagName);

el.Coords = shp.Coords;

el.Color = shp.Color;

el.Color2 = shp.Color2;

el.Gradient = shp.Gradient;

el.PenWidth = shp.PenWidth;

el.ShapeType = shp.Type;

el.StartAngle = shp.StartAngle;

el.SweepAngle = shp.SweepAngle;

el.ID = shp.ID;

return el;

}

//--------------------------------

public string TagName

{

get{ return "shape"; }

}

//--------------------------------

public string Coords

{

get{ return sCoords; }

set{

sCoords = value;

tmpXel.SetAttribute("coords", value);

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public ShapeTypes Type

{

get{ return iType; }

set{

iType = value;

tmpXel.SetAttribute("type", value.ToString());

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public GradientTypes Gradient

{

get{ return iGradient; }

set{

iGradient = value;

tmpXel.SetAttribute("gradient", value.ToString());

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public int PenWidth

{

get{ return iPenWidth; }

set{

iPenWidth = value;

tmpXel.SetAttribute("penwidth", value.ToString());

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public int StartAngle

{

get{ return iStartAngle; }

set{

iStartAngle = value;

tmpXel.SetAttribute("type", value.ToString());

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public int SweepAngle

{

get{ return iSweepAngle; }

set{

iSweepAngle = value;

tmpXel.SetAttribute("sweepangle", value.ToString());

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public string Color

{

get{ return sColor; }

set{

sColor = value;

tmpXel.SetAttribute("color", value);

tmpNode = (XmlElement) tmpXel;

}

}

//--------------------------------

public string Color2

{

get{ return sColor2; }

set{

sColor2 = value;

tmpXel.SetAttribute("color2", value);

tmpNode = (XmlElement) tmpXel;

}

}

public string ID

{

get{ return sID; }

set{

sID = value;

tmpXel.SetAttribute("ID", value);

tmpNode = (XmlElement) tmpXel;

}

}

}