, , . XML .
<bag>
<circle id="1" radius="5">Circle 1</circle> />
<square id="2" length="3">Square 1</square> />
<rectangle id="3" length="6" width="7">Rectangle 1</rectangle>/>
</bag>
. , . - XElement, "".
public abstract class shape
{
private readonly string _ID;
public string id
{
get
{
return _ID;
}
}
public string Name { get; set; }
public shape(string id, string name)
{
_ID = id;
this.Name = name;
}
public shape(XElement element)
{
_ID = element.Attribute("id").Value;
this.Name = element.Value;
}
public abstract XElement GetXElement();
public abstract double Area();
}
. , (). , GetXElement XML node, XML.
public class circle : shape
{
public int Radius { get; set; }
public circle(string id, string name, int radius)
: base(id, name)
{
this.Radius = radius;
}
public circle(XElement element)
: base(element)
{
this.Radius = int.Parse(element.Attribute("radius").Value);
}
public override XElement GetXElement()
{
return new XElement("circle", new XAttribute("id", this.id), new XAttribute("radius", this.Radius), this.Name);
}
public override double Area()
{
return Math.PI * Radius * Radius;
}
}
, , Open/Closed. , XML , Console.WriteLine(element) XML . , , , XML .
public void TestMethod1()
{
var doc = XDocument.Load(xmlFile);
double area=0;
foreach (var shapeItem in doc.Descendants("bag").Descendants())
{
var type = Type.GetType("StackOverflowShapes." + shapeItem.Name + ",StackOverflowShapes");
var myShape = (shape)Activator.CreateInstance(type, shapeItem);
area += myShape.Area();
var element = myShape.GetXElement();
Console.WriteLine(element);
}
Assert.Equal(129.5398, area, 4);
}
. , XML XML.