C# 导入xml并更新画布-silverlight
此图像显示项目的功能 我可以将画布保存为xml,如下所示:C# 导入xml并更新画布-silverlight,c#,xml,silverlight,import,C#,Xml,Silverlight,Import,此图像显示项目的功能 我可以将画布保存为xml,如下所示: private void SaveFile(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog() { DefaultExt = "xml", Filter = "XML Files (*.xml)|*.xml|All files (*.*)|*.*",
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文件,然后用方框和连接更新画布。有什么想法吗?这是一个完整的往返行程,说明了流程:
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文档的方法(稍作自定义):
用户选择文件并加载。下面是一个完整的往返过程,说明了该过程:
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文档的方法(稍作自定义):
用户选择文件并将其加载。但这是为了在图像上获取该示例,我尝试的是获取一个已创建的xml文件并进行更新,您的示例已经为框设置了名称。。它应该加载xml文件并用数据更新画布,但这只是用最少的管道来说明。因此,您可以跳过创建框和关系的第一部分,直接执行如下操作:
string xml=File.ReadAllText(“my_saved_data.xml”)
或直接XDocument doc=XDocument.Load(“my_saved_data.xml”)
:)我不明白,没有打开的文件或其他什么?wich将打开一个窗口来选择xml文件。。很抱歉,我不太擅长:)我已经更新了我的答案,使用了与SaveFileDialog相反的方法:OpenFileDialog。我无法使用它,我会出现很多错误,我真的不知道我必须剪切什么(对于“livro,autor”部分),或者我必须将OpenFileDialog放在哪里。。这是说找不到XmlTextWriter->“XmlWriter xmlw=new XmlTextWriter(sw);”但这是为了在图像上获取该示例,我试图做的是获取一个创建的xml文件并进行更新,您的示例已经设置了框的名称。。它应该加载xml文件并用数据更新画布,但这只是用最少的管道来说明。因此,您可以跳过创建框和关系的第一部分,直接执行如下操作:string xml=File.ReadAllText(“my_saved_data.xml”)
或直接XDocument doc=XDocument.Load(“my_saved_data.xml”)
:)我不明白,没有打开的文件或其他什么?wich将打开一个窗口来选择xml文件。。很抱歉,我不太擅长:)我已经更新了我的答案,使用了与SaveFileDialog相反的方法:OpenFileDialog。我无法使用它,我会出现很多错误,我真的不知道我必须剪切什么(对于“livro,autor”部分),或者我必须将OpenFileDialog放在哪里。。这是说找不到XmlTextWriter->“XmlWriter xmlw=新的XmlTextWriter(sw);”