保存到xml文件并从中加载 [英] saving to and loading from xml file

查看:79
本文介绍了保存到xml文件并从中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下在代码项目中找到的代码将节点从我的Treeview加载到xml文件,然后再次从xml加载到Windows窗体应用程序上的TreeView.当将我的树视图中的节点加载到xml文件时,根节点的所有子节点都将串联在一起,这导致将所有子节点显示为根节点的一个子节点.任何帮助将不胜感激.

方法如下:

I used the following code found on code project for loading nodes from my Treeview to an xml file, and then again from the xml to the TreeView on my windows form application. when the nodes from my treeview are loaded to the xml file, all the children of the root node are being concatenated, which results in displaying all the children nodes as one single child node of the root node. Any help would be greatly appreciated.

Here is the methods:

public void SaveTree(TreeView tv, string FileName)
        {
            xmlWriter = new XmlTextWriter(FileName, System.Text.Encoding.UTF8);

            xmlWriter.WriteStartDocument();

            //writes the root node to xml document
            xmlWriter.WriteStartElement(this.treeView1.Nodes[0].Text);

            foreach (TreeNode node in tv.Nodes)
            {
                SaveNode(node.Nodes);
            }

            xmlWriter.Flush();
            xmlWriter.Close();
        }

        public void SaveNode(TreeNodeCollection tnc)
        {
            foreach (TreeNode tNode in tnc)
            {
                if (tNode.Nodes.Count > 0)
                {
                    foreach (TreeNode tNode2 in tNode.Nodes)
                    {
                        xmlWriter.WriteString(tNode2.Text);
                        
                        SaveNode(tNode.Nodes);
                    }
                }
                else
                {
                    xmlWriter.WriteString(tNode.Text);
                    
                }
            }
        }

        public void PopulateTreeView()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(FileName);

                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));


                TreeNode tNode = new TreeNode();
                tNode = (TreeNode)this.treeView1.Nodes[0];

                AddTreeNode(xDoc.DocumentElement, tNode);

                this.Cursor = Cursors.Default;
            }
            catch (XmlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void AddTreeNode(XmlNode xmlNode, TreeNode treeNode)
        {
            XmlNode XMLNode;
            TreeNode Tnode;
            XmlNodeList NodeList;

            if (xmlNode.HasChildNodes)
            {
                NodeList = xmlNode.ChildNodes;

                for (int i = 0; i <= NodeList.Count - 1; i++)
                {
                    XMLNode = xmlNode.ChildNodes[i];
                    treeNode.Nodes.Add(new TreeNode(XMLNode.Name));
                    Tnode = treeNode.Nodes[i];
                    AddTreeNode(XMLNode, Tnode);
                }
            }
            else
            {
                treeNode.Text = xmlNode.OuterXml.Trim();
            }
        }



这是AddNode方法:



Here is the AddNode method:

public void AddNode()
{
if (this.treeView1.SelectedNode != null)
{
NewNode newNode = new NewNode();
newNode.ShowDialog();

TreeNode tNode = new TreeNode();
tNode.Name = newNode.nName.ToString();
tNode.Text = newNode.nText.ToString();

this.treeView1.SelectedNode.Nodes.Add(tNode);
}
else
{

NewNode neNode = new NewNode();
neNode.ShowDialog();

TreeNode tnode = new TreeNode();
tnode.Name = neNode.nName.ToString();
tnode.Text = neNode.nText.ToString();

this.treeView1.Nodes.Add(tnode);
}

this.treeView1.ExpandAll();
}



例如,如果要添加的第一个节点是"haha​​ha",并在其上添加三个子节点a,b,c,则在调试模式下查看时一切正常,但是将数据写入xml在文件中,根节点被视为单个节点,但是如果根节点的子级没有子级,则它们是级联的!如果孩子没有孩子,这可能是因为我将文本设置为OuterXml. xmlnode没有Text属性,只有Name属性,并且在选择Name属性时,它仅返回#text.所以我的问题是如何检索xmlNode的text属性?



if, for instance, the first node to be added is "hahaha", and on to that, I add three child nodes a, b, c, everything works fine when viewing in debug mode, but when the data is written to the xml file, the root node is seen as a single node, but if the root node''s children have no children, they are concatenated! This might be as I made the text OuterXml if the children have no children. The xmlnode has no Text property, only a Name property, and when choosing the Name property, it only returns #text. So my question is how do I retrieve the text property of the xmlNode?

推荐答案

我找不到AddNode方法,但是我可以在您的AddTreeNode中看到它方法,您不仅要添加它而且要递归.您是否已遍历代码以查看发生了什么?您是说您的XML有多个级别,但是它们都放在同一级别上吗?听起来您需要在AddTreeNode方法中使用断点以查看发生了什么.同样,制作文本OuterXml意味着使单个节点等于从此以后的所有XML.您肯定只需要该节点的文本吗?
I can''t find the AddNode method, but I can see that in your AddTreeNode method, you add it as well as recursing. Have you stepped through the code to see what''s happening ? Are you saying your XML has multiple levels but they all get put on at the same level ? Sounds like you need a breakpoints in your AddTreeNode method to see what is going on. Also, making the text OuterXml, means making a single node equal to all the XML from there down. You want the text of just that node, surely ?


确定,我删除了您的回复"并将其粘贴到您的问题中.首先,XmlNode是基类.您的节点是XmlElement,它具有您需要的属性.除此之外,我并没有真正了解问题所在,XML是否能反映您的期望?是阅读或写作方面的问题吗?如果是书面形式,也许是您发布了XML以及有关它未显示所需内容的解释?
OK, I deleted your ''reply'' and pasted it in to your question. First of all, XmlNode is a base class. Your node is an XmlElement, which has the properties you need. Beyond that, I''m not really following what''s going wrong, does the XML reflect what you expect it to ? Is the problem in reading or writing ? If it''s in writing, perhaps if you posted the XML as well as an explanation of how it''s not showing what you want it to ?


确定要减少吗?级别的XML编程?该问题已经为您很好地解决了.

我建议您改用数据合同.它的序列化器将自动存储和还原所有数据,不仅是树,还包括任意结构的对象图(这意味着它可以包含循环引用,序列化器将正确处理).另外,XML代码非常易于阅读,模式可以是世界唯一的(为此目的,请使用基于公司网站的名称空间),并且该技术是最不介入的—您只需向数据类型和成员添加属性(通常是[DataContract][DataMember]).

请参阅: http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

另请参阅我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Are you sure you need low-level XML programming? The problem is already nicely solved for you.

I would suggest you use Data Contract instead. Its serializer will automatically store and restore all your data, not just tree, but an object graph of the arbitrary structure (it means, it can contain circular references, the serializer will handle it correctly). Also, the XML code is very human-readable, the schema can be world-unique (use your company site based name space for this purpose) and the technique is the most non-intrusive — you only add attributes to the data types and members (usually [DataContract] and [DataMember]).

See: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

—SA


这篇关于保存到xml文件并从中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆