Here is my solution ... it looks like I thought it was too confusing before I really sat down and looked at him:
private StrokeCollection CreateStrokeCollectionfromXML(string XMLStrokes)
{
XElement xmlElem;
try
{
xmlElem = XElement.Parse(XMLStrokes);
}
catch (XmlException ex)
{
return new StrokeCollection();
}
StrokeCollection objStrokes = new StrokeCollection();
var strokes = from s in xmlElem.Descendants("Stroke") select s;
foreach (XElement strokeNodeElement in strokes)
{
var color = (from c in strokeNodeElement.Descendants("Color")
select c).FirstOrDefault();
DrawingAttributes attributes = new DrawingAttributes();
byte colorA = Convert.ToByte(color.Attribute("A").Value);
byte colorR = Convert.ToByte(color.Attribute("R").Value);
byte colorG = Convert.ToByte(color.Attribute("G").Value);
byte colorB = Convert.ToByte(color.Attribute("B").Value);
attributes.Color = Color.FromArgb(colorA, colorR, colorG, colorB);
var outlineColor = (from oc in strokeNodeElement.Descendants("OutlineColor")
select oc).FirstOrDefault();
byte outlineColorA = Convert.ToByte(outlineColor.Attribute("A").Value);
byte outlineColorR = Convert.ToByte(outlineColor.Attribute("R").Value);
byte outlineColorG = Convert.ToByte(outlineColor.Attribute("G").Value);
byte outlineColorB = Convert.ToByte(outlineColor.Attribute("B").Value);
attributes.OutlineColor = Color.FromArgb(outlineColorA, outlineColorR, outlineColorG, outlineColorB);
attributes.Width = Convert.ToInt32(strokeNodeElement.Descendants("Width").FirstOrDefault().Value);
attributes.Height = Convert.ToInt32(strokeNodeElement.Descendants("Height").FirstOrDefault().Value);
var points = from p in strokeNodeElement.Descendants("Point")
select p;
StylusPointCollection pointData = new System.Windows.Input.StylusPointCollection();
foreach (XElement point in points)
{
double Xvalue = Convert.ToDouble(point.Attribute("X").Value);
double Yvalue = Convert.ToDouble(point.Attribute("Y").Value);
pointData.Add(new StylusPoint(Xvalue, Yvalue));
}
Stroke newstroke = new Stroke();
newstroke.DrawingAttributes = attributes;
newstroke.StylusPoints.Add(pointData);
objStrokes.Add(newstroke);
}
return objStrokes;
}
source
share