解析旧的xml以创建一个新的单文档已经有一个根节点c#

时间:2018-01-10 15:22:53

标签: c# xml treeview xmldocument

我有一个上传到treeView的XML文件,我必须通过将旧节点视为文本并用新标签包装然后将它们全部保存到文件中来创建新的XML。我必须记住哪些节点是子节点,并为它们提供元素标记。

        private void saveNode(TreeNode node, XmlNode xnode,XmlDocument xmlDoc1)
        {


            foreach (TreeNode tnode in treeView1.Nodes)
            {


                        xnode.AppendChild(xmlDoc1.CreateNode(XmlNodeType.Text, tnode.Text, tnode.Text));

                        XmlNode rootNode = xmlDoc1.CreateElement("category");

                        XmlAttribute oid = rootNode.Attributes.Append(xmlDoc1.CreateAttribute("oid"));
                        oid.Value = Guid.NewGuid().ToString();
                        xmlDoc1.AppendChild(rootNode);

                        XmlElement dr = (XmlElement)rootNode.AppendChild(xmlDoc1.CreateElement("name"));
                        dr.AppendChild(xmlDoc1.CreateTextNode("Model"));
                        XmlElement com = (XmlElement)rootNode.AppendChild(xmlDoc1.CreateElement("comment"));
                        XmlElement condition = (XmlElement)rootNode.AppendChild(xmlDoc1.CreateElement("condition"));

                        xmlDoc1.AppendChild(rootNode);

                        saveNode(tnode, xnode,xmlDoc1);

                        if (node.NextNode != null)
                        {
                            foreach (TreeNode tn in treeView1.Nodes)
                            {

                                XmlNode root1 = xmlDoc1.CreateElement("elements");
                                root1.AppendChild(xmlDoc1.CreateNode(XmlNodeType.Text, tnode.Text, "elements"));
                                root1.InnerText = tnode.Text;
                                saveNode(tn, xnode, xmlDoc1);

                            }
                        }
                        else {
                            foreach (TreeNode tn in treeView1.Nodes)
                            {
                                XmlNode root1 = xmlDoc1.CreateElement("elements");
                                root1.AppendChild(xmlDoc1.CreateNode(XmlNodeType.Text, tnode.Text, "elements"));
                                saveNode(tn, xnode, xmlDoc1);
                            }
                        }

               }

        }



        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {


            XmlDocument xmlDoc1 = new XmlDocument();


            XmlNode xroot = xmlDoc1.CreateElement("model");
            XmlAttribute oid = xroot.Attributes.Append(xmlDoc1.CreateAttribute("oid"));
            oid.Value = Guid.NewGuid().ToString();
            xmlDoc1.AppendChild(xroot);

            XmlElement dr = (XmlElement)xroot.AppendChild(xmlDoc1.CreateElement("name"));
            dr.AppendChild(xmlDoc1.CreateTextNode("Model"));
            XmlElement com = (XmlElement)xroot.AppendChild(xmlDoc1.CreateElement("comment"));
            XmlElement condition = (XmlElement)xroot.AppendChild(xmlDoc1.CreateElement("condition"));
            XmlElement el = (XmlElement)xroot.AppendChild(xmlDoc1.CreateElement("element"));

            try
            {
                foreach (TreeNode tn in treeView1.Nodes)
                {
                    saveNode(tn, xroot,xmlDoc1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


            saveFileDialog1.Filter = "Text Files (*.xml)|*.xml";
            saveFileDialog1.AddExtension = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                xmlDoc1.Save(saveFileDialog1.FileName);
            }



        }

0 个答案:

没有答案