Silverlight 3:将 XML 笔划集合转换回笔划集合?

发布于 2024-08-07 10:00:30 字数 584 浏览 3 评论 0原文

我有一个 silverlight 应用程序,允许用户在其上绘图并保存绘图。

画布中的笔画集合被转换为xml属性并存储在数据库中。

我现在遇到的唯一问题是将 xml 转换回笔画集合。

我的笔画存储如下:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-08-14 10:00:30

这是您的代码,经过严格考虑:-

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);       
}

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);       
}
沩ん囻菔务 2024-08-14 10:00:30

这是我的解决方案......在我真正坐下来查看它之前,我似乎认为它太混乱了:

private StrokeCollection CreateStrokeCollectionfromXML(string XMLStrokes)
    {
        XElement xmlElem;
        try
        {
            xmlElem = XElement.Parse(XMLStrokes);
        }
        catch (XmlException ex)
        {
            return new StrokeCollection();
        }
        StrokeCollection objStrokes = new StrokeCollection();
        //Query the XML to extract the Strokes
        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);
            //add the new stroke to the StrokeCollection
            objStrokes.Add(newstroke);
        }
        return objStrokes;
    }

Here is my solution... it appears I thought it was too confusing before I actually sat down and looked at it:

private StrokeCollection CreateStrokeCollectionfromXML(string XMLStrokes)
    {
        XElement xmlElem;
        try
        {
            xmlElem = XElement.Parse(XMLStrokes);
        }
        catch (XmlException ex)
        {
            return new StrokeCollection();
        }
        StrokeCollection objStrokes = new StrokeCollection();
        //Query the XML to extract the Strokes
        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);
            //add the new stroke to the StrokeCollection
            objStrokes.Add(newstroke);
        }
        return objStrokes;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文