C#讀寫檔案--專案案例
阿新 • • 發佈:2019-02-04
//寫檔案–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(.
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<string> list = new List<string>(); //選擇所有的第二節點 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); } } } } } }
“`