Wie serialisieren TreeView in xml und Deserialisieren von xml wieder in TreeView?

Wie serialisieren TreeView in xml und Deserialisieren von xml wieder in TreeView?

Nach dem laden-Elemente und-Attribute die folgende xml-Datei im treeview-Knoten bearbeitet und die treeview gespeichert ist, wieder in der gleichen xml-Datei. Alle Elemente und Attribute gespeichert werden müssen. Aber die Attribute nur verschachtelte Elemente verschwinden beim speichern. Nach dem speichern werden alle attribures der Elemente, die d & e sind verloren! Das ist, weil ich bin nicht in der Lage zum abrufen der Attribut gespeicherten Werte in die tag-Eigenschaft in addTreeNode Funktion.(siehe inline-Kommentare) kennt jemand eine einfachere oder bessere Möglichkeit, dies zu erreichen? Die Bereitstellung von code-snippets, wäre hilfreich.

XML-Struktur:

<?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# - code:

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();  
InformationsquelleAutor Martin | 2014-01-02
Schreibe einen Kommentar