如何将TreeView序列化为xml并反序列化xml回到TreeView? [英] How to serialize TreeView in to xml and deserialize xml back to TreeView?

查看:107
本文介绍了如何将TreeView序列化为xml并反序列化xml回到TreeView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将以下xml文件的元素和属性加载到树视图中之后,将编辑节点并将树视图保存回相同的xml文件中.所有元素和属性都需要保存.但是,只有嵌套元素的属性在保存期间消失.保存后,元素d&的所有属性e迷路了!这是因为我无法在addTreeNode函数中检索存储在tag属性中的属性值.(请参阅内联注释)有人知道实现此目标的更简单或更简洁的方法吗?提供代码段将很有帮助.

After loading elements and attributes of the below xml file in to a treeview, the nodes are edited and the treeview is saved back in to the same xml file. All elements and attributes need to be saved. However the attributes of only nested elements disappear during saving. After saving, all attribures of elements d & e are lost! This is because i am unable to retrieve the attribute values stored in to the tag property in addTreeNode function.(please see inline comments) Does anyone know of an easier or cleaner way to achieve this? Providing code snippets would be helpfull.

XML结构:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <a axa="1" axb="2" axc="3">content_of_tag _a</a>
  <b bxa="10" bxb="20" bxc="30">content_of_tag_b</b>
  <c cxa="11" cxb="21" cxc="31">
  content_of_tag_c
      <d dxa="101" dxb="201" dxc="301">
      content_of_tag_d
          <e exa="110" exb="210" exc="310">
          content_of_tag_e
          </e>
      </d>
  </c>
</root>  

C#代码:

private void Xml2TreeNode(XElement xNode, TreeNode treeNode)
{
    if (xNode.HasElements) //if node has children
    {
        TreeNode tNode = null;
        int i = 0;
        foreach (XElement subNode in xNode.Elements())
        {
            if (subNode.Descendants().Count() > 0)
            {
                TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim());
                tn.Nodes.Add(new TreeNode(subNode.FirstNode.ToString().Trim()));
                treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //-------->this attribure values are NOT retrievable in saveNodes function!
                tNode = tn; //add child nodes
            }
            else
            {
                TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim()); //show name of a leaf node element
                tn.Nodes.Add(new TreeNode(subNode.Value.ToString().Trim())); //show value of above element as a child of its name
                treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrivable in saveNodes function
                tNode = treeNode.Nodes[i++]; //add sibling node
            }

            addTreeNode(subNode, tNode); //recursively add child nodes
        }
    }
}




private void TreeNode2Xml(TreeNodeCollection tnc)
{
    foreach (TreeNode node in tnc)
    {
        if (node.Nodes.Count > 0)
        {
            xr.WriteStartElement(node.Text);
            if (node.Tag != null) //attribures retrieved here
            {
                List<XAttribute> attributeList = node.Tag as List<XAttribute>;
                foreach (XAttribute attribute in attributeList)
                {
                    xr.WriteAttributeString(attribute.Name.ToString(), attribute.Value.ToString());
                }
            }
            saveNodes(node.Nodes);
            xr.WriteEndElement();
        }
        else //No child nodes, so we just write the text
        {
            xr.WriteString(node.Text);
        }
    }
}  



xr = new XmlTextWriter(filename, System.Text.Encoding.UTF8); //System.Text.Encoding.UTF8
xr.Formatting = Formatting.Indented;
xr.WriteStartDocument();
//Write our root node
xr.WriteStartElement(treeView1.Nodes[0].Text);
foreach (TreeNode node in tv.Nodes)
{
    **TreeNode2Xml(node.Nodes);**
}
//Close the root node
xr.WriteEndElement();
xr.Close();




xDoc = XDocument.Load(dlg.FileName);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xDoc.Document.Root.Name.ToString().Trim()));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)treeView1.Nodes[0];
**Xml2TreeNode(xDoc.Root, tNode);**
treeView1.ExpandAll();  

推荐答案

通过在Xml2TreeNode()函数中进行小的更改来解决此问题

Fixed the issue by making small changes in Xml2TreeNode() function

private void Xml2TreeNode(XElement xNode, TreeNode treeNode)
{
    if (xNode.HasElements) //if node has children
    {
        TreeNode tNode = null;
        int i = 0;
        foreach (XElement subNode in xNode.Elements())
        {
            if (subNode.Descendants().Count() > 0)
            {//handle non-leaf node
                TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim());
                tn.Nodes.Add(new TreeNode(subNode.FirstNode.ToString().Trim()));
                tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in SaveNodes function
                tNode = tn; //add child nodes
            }
            else
            {//handle leaf node
                TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim()); //show name of a leaf node element
                tn.Nodes.Add(new TreeNode(subNode.Value.ToString().Trim())); //show value of above element as a child of its name
                tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in SaveNodes function
                tNode = treeNode.Nodes[i++]; //add sibling node
            }

            Xml2TreeNode(subNode, tNode); //recursively add child nodes
        }
    }
}  



private void Xml2TreeNode(XElement xNode, TreeNode treeNode)
{
    if (xNode.HasElements) //if node has children
    {
        TreeNode tNode = null;
        int i = 0;
        foreach (XElement subNode in xNode.Elements())
        {
            if (subNode.Descendants().Count() > 0)
            {//handle non-leaf node
                TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim());
                ////tn.Nodes.Add(new TreeNode(subNode.FirstNode.ToString().Trim())); //adds extra element-value to node
                tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in TreeNode2Xml function
                tNode = tn; //add child nodes
            }
            else
            {//handle leaf node
                TreeNode tn = treeNode.Nodes.Add(subNode.Name.ToString().Trim()); //show name of a leaf node element
                tn.Nodes.Add(new TreeNode(subNode.Value.ToString().Trim())); //show value of above element as a child of its name
                tn.Tag = treeNode.Nodes[i].Tag = subNode.Attributes().ToList(); //---->these values are retrived in TreeNode2Xml function
                tNode = treeNode.Nodes[i++]; //add sibling node
            }

            Xml2TreeNode(subNode, tNode); //recursively add child nodes
        }
    }
}

这篇关于如何将TreeView序列化为xml并反序列化xml回到TreeView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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