1

这张图片显示了项目的作用

在此处输入图像描述

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

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 文件,然后用框和连接更新画布。有任何想法吗?

4

1 回答 1

1

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

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

希望这可以帮助...

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

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

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

用户选择文件并加载它。

于 2013-06-20T21:20:37.007 回答