/// <summary> /// 遍历treeview并实现向XML的转化 /// </summary> /// <param name="TheTreeView">树控件对象</param> /// <param name="XMLFilePath">XML输出路径</param> /// <returns>0表示函数顺利执行</returns> public int TreeToXML(TreeView TheTreeView, string XMLFilePath) { //-------初始化转换环境变量 thetreeview = TheTreeView; xmlfilepath = XMLFilePath; textWriter = new XmlTextWriter(xmlfilepath, null); //-------创建XML写操作对象 textWriter.Formatting = Formatting.Indented; //-------开始写过程,调用WriteStartDocument方法 textWriter.WriteStartDocument(); ////-------添加第一个根节点 textWriter.WriteStartElement((thetreeview.Nodes[0].Tag as 剧本编辑器.AttType).Type); 剧本编辑器.AttType attType = new 剧本编辑器.AttType(); Dictionary<string, string> dic = new Dictionary<string, string>() { {"A","Root"},{"B","Root"}, {"E","0000000000000000"},{"N","Default"}, {"C","Default"}, {"D","Default"}, {"F","Default"},{"G","0"}, {"H","Default"},{"I","0"}, {"J","Default"},{"K","0"}, {"L","Default"},{"M","0"} }; foreach (KeyValuePair<string, string> pair in dic) { textWriter.WriteStartAttribute(pair.Key); textWriter.WriteString(pair.Value); textWriter.WriteEndAttribute(); } textWriter.WriteEndElement(); //------ 写文档结束,调用WriteEndDocument方法 textWriter.WriteEndDocument(); //-----关闭输入流 textWriter.Close(); //-------创建XMLDocument对象 textdoc.Load(xmlfilepath); //------选中根节点 XmlElement Xmlnode = textdoc.CreateElement(thetreeview.Nodes[0].Text); Xmlroot = textdoc.SelectSingleNode((thetreeview.Nodes[0].Tag as 剧本编辑器.AttType).Type); //------遍历原treeview控件,并生成相应的XML TransTreeSav(thetreeview.Nodes[0].Nodes, (XmlElement)Xmlroot); textdoc.Save(xmlfilepath); return 0; }
private void 预览_Load(object sender, EventArgs e) { 剧本编辑器.AttType att = null; this.AutoScroll = true; this.HorizontalScroll.Enabled = true; this.VerticalScroll.Enabled = true; this.HorizontalScroll.Visible = true; this.VerticalScroll.Visible = true; for (int i = 0; i < treeNode.Nodes.Count; i++) { //Label lbContent = null; //att = treeNode.Nodes[i].Tag as 剧本编辑器.AttType; //if (att.Pos == "Left") //{ // Label lbLeft = new Label(); // lbContent = new Label(); // lbContent.Text = att.Content; // if (att.IsOption) // { // lbContent.Text = att.Content + "\r\n" + att.OptionName1 + ":" + att.OptionAction1 + "\r\n" + // att.OptionName2 + ":" + att.OptionAction2 + "\r\n" + att.OptionName3 + ":" + // att.OptionAction3 + "\r\n" + att.OptionName4 + ":" + att.OptionAction4; // } // lbLeft.Top = 10 + i * 60; // lbLeft.Left = 30; // lbContent.Top = lbLeft.Top+lbLeft.Height + 10; // lbContent.Left = 30; // lbLeft.Text = att.SpeakerName + ":"; // lbContent.AutoSize = true; // this.Controls.Add(lbLeft); //} //else //{ // Label lbRight = new Label(); // lbContent = new Label(); // lbContent.Text = att.Content; // if (att.IsOption) // { // lbContent.Text = att.Content + "\r\n" + att.OptionName1 + ":" + att.OptionAction1 + "\r\n" + // att.OptionName2 + ":" + att.OptionAction2 + "\r\n" + att.OptionName3 + ":" + // att.OptionAction3 + "\r\n" + att.OptionName4 + ":" + att.OptionAction4; // } // lbRight.Top = lbContent.Top+lbContent.Height+10; // lbRight.Left = lbContent.Left + lbContent.Width + 10; // lbRight.Text = ":" + att.SpeakerName; // lbContent.Top = lbRight.Top+lbRight.Height+10; // lbContent.Left = 30; // lbContent.AutoSize = true; // this.Controls.Add(lbRight); //} //this.Controls.Add(lbContent); att = treeNode.Nodes[i].Tag as 剧本编辑器.AttType; if (att.Pos == "Left") { tbView.Text += att.SpeakerName + ":\r\n"; tbView.Text += " " + att.Content + "\r\n\r\n"; if (att.IsOption) { tbView.Text += att.OptionName1 + (att.OptionName1 == "" ? "" : ":") + (att.OptionName1 == "" ? "" : (int.Parse(att.OptionAction1) + 1).ToString()) + "\r\n" + att.OptionName2 + (att.OptionName2 == "" ? "" : ":") + (att.OptionName2 == "" ? "" : (int.Parse(att.OptionAction2) + 1).ToString()) + "\r\n" + att.OptionName3 + (att.OptionName3 == "" ? "" : ":") + (att.OptionName3 == "" ? "" :(int.Parse(att.OptionAction3) + 1).ToString()) + "\r\n" + att.OptionName4 + (att.OptionName4 == "" ? "" : ":") + (att.OptionName4 == "" ? "" :(int.Parse(att.OptionAction4) + 1).ToString()) + "\r\n\r\n"; } } else { tbView.Text += " :" + att.SpeakerName + "\r\n"; tbView.Text += " " + att.Content + "\r\n\r\n"; if (att.IsOption) { tbView.Text += att.OptionName1 + (att.OptionName1 == "" ? "" : ":") + (att.OptionName1 == "" ? "" : (int.Parse(att.OptionAction1) + 1).ToString()) + "\r\n" + att.OptionName2 + (att.OptionName2 == "" ? "" : ":") + (att.OptionName2 == "" ? "" : (int.Parse(att.OptionAction2) + 1).ToString()) + "\r\n" + att.OptionName3 + (att.OptionName3 == "" ? "" : ":") + (att.OptionName3 == "" ? "" : (int.Parse(att.OptionAction3) + 1).ToString()) + "\r\n" + att.OptionName4 + (att.OptionName4 == "" ? "" : ":") + (att.OptionName4 == "" ? "" : (int.Parse(att.OptionAction4) + 1).ToString()) + "\r\n\r\n"; } } } }
private int TransTreeSav(TreeNodeCollection nodes, XmlElement ParXmlnode) { //-------遍历树的各个故障节点,同时添加节点至XML XmlElement xmlnode; //string nodeName = (thetreeview.Nodes[0].Tag as 剧本编辑器.AttType).Type; //if (nodeName == "Dialog") //{ Xmlroot = textdoc.SelectSingleNode((thetreeview.Nodes[0].Tag as 剧本编辑器.AttType).Type); 剧本编辑器.AttType att = new 剧本编辑器.AttType(); foreach (TreeNode node in nodes) { xmlnode = textdoc.CreateElement((node.Tag as 剧本编辑器.AttType).Type); att = node.Tag as 剧本编辑器.AttType; if (xmlnode.Name == "Dialog") { att.ScenesID = node.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Index; att.ChapterID = node.Parent.Parent.Parent.Parent.Parent.Parent.Index; att.LevelID = node.Parent.Parent.Parent.Parent.Parent.Index; att.DifficultyID = node.Parent.Parent.Parent.Parent.Index; att.ActorID = node.Parent.Parent.Parent.Index; att.BattleID = node.Parent.Parent.Index; att.PointIndex = node.Parent.Index; string Info = att.ScenesID < 10 ? "0" + att.ScenesID : att.ScenesID.ToString(); Info += att.ChapterID < 10 ? "0" + att.ChapterID : att.ChapterID.ToString(); Info += att.LevelID < 10 ? "0" + att.LevelID : att.LevelID.ToString(); Info += att.DifficultyID < 10 ? "0" + att.DifficultyID : att.DifficultyID.ToString(); Info += att.ActorID < 10 ? "0" + att.ActorID : att.ActorID.ToString(); Info += att.BattleID <10 ? "0" + att.BattleID : att.BattleID.ToString(); Info += att.PointIndex <10 ? "0" + att.PointIndex : att.PointIndex.ToString(); Info += att.Pos == "Left" ? "1" : "0"; Info += att.IsOption ? "1" : "0"; xmlnode.SetAttribute("A", att.Type); xmlnode.SetAttribute("B", att.Tag); xmlnode.SetAttribute("C", att.SpeakerName); xmlnode.SetAttribute("D", att.Content); xmlnode.SetAttribute("E", Info); xmlnode.SetAttribute("F", att.OptionName1); xmlnode.SetAttribute("G", att.OptionAction1); xmlnode.SetAttribute("H", att.OptionName2); xmlnode.SetAttribute("I", att.OptionAction2); xmlnode.SetAttribute("J", att.OptionName3); xmlnode.SetAttribute("K", att.OptionAction3); xmlnode.SetAttribute("L", att.OptionName4); xmlnode.SetAttribute("M", att.OptionAction4); xmlnode.SetAttribute("N", att.SpeakerProfile); } else { xmlnode.SetAttribute("A", att.Type); xmlnode.SetAttribute("B", att.Tag); } ParXmlnode.AppendChild(xmlnode); if (node.Nodes.Count > 0) { TransTreeSav(node.Nodes, xmlnode); } } return 0; }