4

Silverlight 3: Conversion of XML trajectory collection in one second?

 2 years ago
source link: https://www.codesd.com/item/silverlight-3-conversion-of-xml-trajectory-collection-in-one-second.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Silverlight 3: Conversion of XML trajectory collection in one second?

advertisements

I have a silverlight app that allows the user to draw on it and save the drawing.

The strokecollection in the canvas is converted to xml attributes and stored in the database.

the only problem i have now is converting the xml back into a stroke collection.

my strokes are stored as such:

<Strokes>
  <Stroke>
    <Color A="255" R="0" G="0" B="0" />
    <OutlineColor A="0" R="0" G="0" B="0" />
  <Points>
    <Point X="60" Y="57" PressureFactor="0.5" />
    <Point X="332" Y="52" PressureFactor="0.5" />
  </Points>
  <Width>3</Width>
  <Height>3</Height>
  </Stroke>
</Strokes>


Here is your code heavily factored:-

private StrokeCollection CreateStrokeCollection(string XMLStrokes)
{
  XElement xmlElem;
  try
  {
    return CreateStrokeCollect(XElement.Parse(XMLStrokes).Root);
  }
  catch (XmlException ex)
  {
    return new StrokeCollection();
  }
}

private StrokeCollection CreateStrokeCollection(XElement elem)
{
  StrokeCollection result= new StrokeCollection();
  foreach (XElement elem in xmlElement.Elements("Stroke"))
  {
     result.Add(CreateStroke(elem));
  }
  return result;
}

private Stroke CreateStroke(XElement elem)
{
  var result = new Stroke();
  result.DrawingAttributes = CreateAttributes(elem);
  result.StylusPoints.Add(CreatePointCollection(elem.Element("Points"));
  return result;
}

private StylusPointCollection CreatePointCollection(XElement elem)
{
  var result = new StylusPointCollection();
  foreach (XElment pointElem in elem.Elements("Point"))
  {
    result.Add(CreateStylusPoint(pointElem));
  }
  return result;
}

private StylusPoint CreateStylusPoint(XElement elem)
{
    double x = Convert.ToDouble(point.Attribute("X").Value);
    double y = Convert.ToDouble(point.Attribute("Y").Value);
    return new StylusPoint(x, y);
}

private DrawingAttributes CreateAttributes(XElement elem)
{
   var result = new DrawingAttributes();
   result.Color = CreateColor(elem.Element("Color"));
   result.OutlineColor = CreateColor(elem.Element("OutlineColor"));
   result.Width = Convert.ToInt32(elem.Element("Width").Value);
   result.Height = Convert.ToInt32(elem.Element("Height").Value);
   return result ;
}

private Color CreateColor(XElement elem)
{
  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);

  return Color.FromArgb(colorA, colorR, colorG, colorB);
}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK