导入xml并更新画布 - silverlight [英] import xml and update canvas - silverlight

查看:108
本文介绍了导入xml并更新画布 - silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此图片显示项目的作用

我可以像这样将画布保存到xml:

I can save the canvas to xml like this:

private void SaveFile(object sender, RoutedEventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog()
    {
        DefaultExt = "xml",
        Filter = "XML Files (*.xml)|*.xml|All files (*.*)|*.*",
        FilterIndex = 1
    };
    if (saveFileDialog.ShowDialog() == true)
    {
        using (Stream stream = saveFileDialog.OpenFile())
        {
            using (StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8))
            {
                sw.Write(GetGeneratedXML().ToString());
            }
        }
    }
}

private XElement GetGeneratedXML()
{
    XElement userInformation = new XElement("Diagrama");
    foreach (MyBox b in boxes)
    {
        userInformation.Add(new XElement("Entidade",
            new XElement("Nome", b.Header),
            new XElement("Atributo", b.Text)));
    }
    foreach (Connection1 c in connections)
    {
        userInformation.Add(new XElement("Relação",
            new XElement("Entidade1",
                new XAttribute("Nome", c.Box1.Header),
                new XAttribute("Cardinalidade", c.Node1.Title)),
                new XElement("Entidade2",
                    new XAttribute("Nome", c.Box2.Header),
                    new XAttribute("Cardinalidade", c.Node2.Title)
                )
            )
        );
    }
    return userInformation;
}

现在,我想导入这个XML文件,然后更新Canvas与盒子和连接。任何想法?

And now, I would like to import this XML file and then update the Canvas with the boxes and connections. Any Ideas?

推荐答案

这是一个完整的往返程序,说明了这个过程:

Here is a complete roundtrip that illustrates the process:

private static void TestGeneratedXML()
{
    MyBox livro = new MyBox { Header = "Livro", Text = "Nome\r\nAutor" };
    MyBox autor = new MyBox { Header = "Autor", Text = "Nome" };

    Connection hasAutors = new Connection
    {
        Box1 = livro,
        Box2 = autor,
        Node1 = new Node { Title = "1" },
        Node2 = new Node { Title = "*" }
    };

    MyBox[] boxes = { livro, autor };

    Connection[] connections = { hasAutors };

    XElement userInformation = new XElement("Diagrama");

    foreach (MyBox b in boxes)
    {
        userInformation.Add(new XElement("Entidade",
                                new XElement("Nome", b.Header),
                                new XElement("Atributo", b.Text)));
    }

    foreach (Connection c in connections)
    {
        userInformation.Add(new XElement("Relação",
                                new XElement("Entidade1",
                                    new XAttribute("Nome", c.Box1.Header),
                                    new XAttribute("Cardinalidade", c.Node1.Title)),
                                new XElement("Entidade2",
                                    new XAttribute("Nome", c.Box2.Header),
                                    new XAttribute("Cardinalidade", c.Node2.Title))));
    }

    StringWriter sw = new StringWriter();

    XmlWriter xmlw = new XmlTextWriter(sw);// { Settings = new XmlWriterSettings { Indent = true } };

    userInformation.WriteTo(xmlw);

    string xml = sw.ToString();

    XDocument doc = XDocument.Parse(xml);
    MyBox[] outBoxes = doc.Root.Elements("Entidade")
                               .Select(e => new MyBox { Header = e.Element("Nome").Value, Text = e.Element("Atributo").Value })
                               .ToArray();

    Connection[] outConnections = doc.Root.Elements("Relação")
                                       .Select(e => new Connection
                                       {
                                           Box1 = outBoxes.Single(b => b.Header == e.Element("Entidade1").Attribute("Nome").Value),
                                           Box2 = outBoxes.Single(b => b.Header == e.Element("Entidade2").Attribute("Nome").Value),
                                           Node1 = new Node { Title = e.Element("Entidade1").Attribute("Cardinalidade").Value },
                                           Node2 = new Node { Title = e.Element("Entidade1").Attribute("Cardinalidade").Value },
                                           Line = new Line()
                                       })
                                       .ToArray();

    foreach (MyBox box in outBoxes)
    {
        box.MouseLeftButtonDown += Box_MouseLeftButtonDown;
        box.MouseLeftButtonUp += Box_MouseLeftButtonUp;
        box.MouseMove += Box_MouseMove;

        canvas.Children.Add(box);
    }

    RefreshLinesPositions();
}

希望这会有所帮助......

Hope this helps...

编辑:这里是从文件中获取XML文档的方法(自定义一点):

EDIT: here is way to obtain the XML document from a file (to customize a little):

OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();

XDocument doc = XDocument.Load(ofd.FileName);

用户选择文件并加载。

这篇关于导入xml并更新画布 - silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆