//写文件–SaveFileDialog
private void Write()
{
string file;
System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
sfd.Filter = "All files(*.*)|*.xml|All files(*.*)|*.*";
sfd.FilterIndex = 1;
sfd.Title = "另存为";
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//注意:下面这一句不要往上写,会报错,原因打开的话,需要手动关闭
// if ((mystream = sfd.OpenFile()) != null)
file = System.IO.Path.GetFullPath(sfd.FileName);
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
//创建最大的子节点
XmlElement OneNode = doc.CreateElement("ChartData");
//下面这一句也挺重要
doc.AppendChild(OneNode);
//得到所有的子控件
UIElementCollection chartList = ChartGrid.Children;
foreach (UIElement chart in chartList)
{
Type type = chart.GetType();
if (type.ToString() == "BlueStarCharts.LineChart2D")
{
LineChart2D L2D = chart as LineChart2D;
XmlElement TwoNode = doc.CreateElement("Data");
TwoNode.SetAttribute("Type", "2d");
TwoNode.SetAttribute("ChartXAxisTitle", L2D.XAxisTitle);
TwoNode.SetAttribute("ChartYAxisTitle", L2D.YAxisTitle);
TwoNode.SetAttribute("Name", L2D.ChartName);
TwoNode.SetAttribute("Theme", L2D.ChartTheme.ToString());
TwoNode.SetAttribute("ChartDDSPartition", L2D.DDSPartition);
TwoNode.SetAttribute("ChartDDSTopic", L2D.DDSTopic);
TwoNode.SetAttribute("ChartDDSLinePartition", L2D.DDSLinePartition);
TwoNode.SetAttribute("ChartDDSLineTopic", L2D.DDSLineTopic);
OneNode.AppendChild(TwoNode);
}
else if (type.ToString() == "BlueStarCharts.LineChart3D")
{
LineChart3D L3D = chart as LineChart3D;
XmlElement TwoNode = doc.CreateElement("Data");
TwoNode.SetAttribute("Type", "3d");
TwoNode.SetAttribute("ChartXAxisTitle", L3D.XAxisTitle);
TwoNode.SetAttribute("ChartYAxisTitle", L3D.YAxisTitle);
TwoNode.SetAttribute("ChartZAxisTitle", L3D.ZAxisTitle);
TwoNode.SetAttribute("Name", L3D.ChartName);
TwoNode.SetAttribute("Theme", L3D.ChartTheme.ToString());
TwoNode.SetAttribute("ChartDDSPartition", L3D.DDSPartition);
TwoNode.SetAttribute("ChartDDSTopic", L3D.DDSTopic);
TwoNode.SetAttribute("ChartDDSLinePartition", L3D.DDSLinePartition);
TwoNode.SetAttribute("ChartDDSLineTopic", L3D.DDSLineTopic);
OneNode.AppendChild(TwoNode);
}
doc.Save(file);
}
}
}
//读文件–OpenFileDialog
private void Read()
{
Stream myStream;
string file;
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Title = “打开”;
ofd.InitialDirectory = @”C:\”;
ofd.Filter = “All files(.)|.xml|All files(.)|.*”;
ofd.FilterIndex = 1;
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = ofd.OpenFile()) != null)
{
//得到文件
file = System.IO.Path.GetFullPath(ofd.FileName);
// file = System.IO.Path.GetExtension(ofd.FileName);
XmlDocument doc = new XmlDocument();
doc.Load(file);
//第一节点
XmlNode OneNode = doc.SelectSingleNode("ChartData");
List list = new List();
//选择所有的第二节点
XmlNodeList TwoNodeList = OneNode.SelectNodes("Data");
for (int i = 0; i < TwoNodeList.Count; i++)
{
string str = ((XmlElement)TwoNodeList[i]).GetAttribute("Type");
list.Add(str);
}
for (int i = 0; i < list.Count; i++)
{
if (list[i] == "2d")
{
New2DChart();
}
if (list[i] == "3d")
{
New3DChart();
}
}
//得到所有的子控件
UIElementCollection chartList = ChartGrid.Children;
//遍历所有的节点
//Test
//遍历所有的节点
for (int i = 0; i < TwoNodeList.Count; i++)
{
//遍历所有的控件
//for (int j = 0; j < chartList.Count; j++)
//{
Type type = chartList[i].GetType();
if (type.ToString() == "BlueStarCharts.LineChart2D")
{
if (((XmlElement)TwoNodeList[i]).GetAttribute("Type") == "2d")
{
LineChart2D L2D = chartList[i] as LineChart2D;
XmlElement twonode = TwoNodeList[i] as XmlElement;
string CXT = twonode.GetAttribute("ChartXAxisTitle");
string CYT = twonode.GetAttribute("ChartYAxisTitle");
string name = twonode.GetAttribute("Name");
string theme = twonode.GetAttribute("Theme");
string DP = twonode.GetAttribute("ChartDDSPartition");
string DT = twonode.GetAttribute("ChartDDSTopic");
string DLP = twonode.GetAttribute("ChartDDSLinePartition");
string DLT = twonode.GetAttribute("ChartDDSLineTopic");
var e = (KSChartThemeEnum)Enum.Parse(typeof(KSChartThemeEnum), theme);
//int ChangeTheme = int.Parse(theme);
L2D.XAxisTitle = CXT;
L2D.YAxisTitle = CYT;
L2D.ChartName = name;
L2D.ChartTheme = e;
L2D.DDSPartition = DP;
L2D.DDSTopic = DT;
L2D.DDSLinePartition = DLP;
L2D.DDSLineTopic = DLT;
//MessageBox.Show(CXT);
//MessageBox.Show(CYT);
}
}
else if (type.ToString() == "BlueStarCharts.LineChart3D")
{
if (((XmlElement)TwoNodeList[i]).GetAttribute("Type") == "3d")
{
LineChart3D L3D = chartList[i] as LineChart3D;
XmlElement twonode = TwoNodeList[i] as XmlElement;
string CXT = twonode.GetAttribute("ChartXAxisTitle");
string CYT = twonode.GetAttribute("ChartYAxisTitle");
string CZT = twonode.GetAttribute("ChartZAxisTitle");
string name = twonode.GetAttribute("Name");
string theme = twonode.GetAttribute("Theme");
string DP = twonode.GetAttribute("ChartDDSPartition");
string DT = twonode.GetAttribute("ChartDDSTopic");
string DLP = twonode.GetAttribute("ChartDDSLinePartition");
string DLT = twonode.GetAttribute("ChartDDSLineTopic");
var e = (KSChartThemeEnum3D)Enum.Parse(typeof(KSChartThemeEnum3D), theme);
//int ChangeTheme = int.Parse(theme);
L3D.XAxisTitle = CXT;
L3D.YAxisTitle = CYT;
L3D.ZAxisTitle = CZT;
L3D.ChartName = name;
L3D.ChartTheme = e;
L3D.DDSPartition = DP;
L3D.DDSTopic = DT;
L3D.DDSLinePartition = DLP;
L3D.DDSLineTopic = DLT;
//MessageBox.Show(CXT);
//MessageBox.Show(CYT);
}
}
}
}
}
}
“`