Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Again : How to save the TreeView

Again : How to save the TreeView

Scheduled Pinned Locked Moved C#
helpxmltutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    j hannemann
    wrote on last edited by
    #1

    Hello again. Now I tried the Save and Load routines from the DUMeter project ! They're very good .. and I only gave my FileName and my TreeView-Control as Object and the saving of the file works fine. And it looks like the samples in the help..... But now as I want to load that file again to fill my TreeView I have a nice Exception : An unhandled exception of type 'System.Xml.XmlException' occurred in system.xml.dll Additional information: System error. I have no idea what causes the error. The file isn't in use and was not edited. He stops at the command where he starts reading the file : System.Xml.XmlTextReader reader = new XmlTextReader(filename); while (reader.Read()) **<-- HERE** { .... Could someone please again help me in this case ? :confused::confused:

    L 1 Reply Last reply
    0
    • J j hannemann

      Hello again. Now I tried the Save and Load routines from the DUMeter project ! They're very good .. and I only gave my FileName and my TreeView-Control as Object and the saving of the file works fine. And it looks like the samples in the help..... But now as I want to load that file again to fill my TreeView I have a nice Exception : An unhandled exception of type 'System.Xml.XmlException' occurred in system.xml.dll Additional information: System error. I have no idea what causes the error. The file isn't in use and was not edited. He stops at the command where he starts reading the file : System.Xml.XmlTextReader reader = new XmlTextReader(filename); while (reader.Read()) **<-- HERE** { .... Could someone please again help me in this case ? :confused::confused:

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      j-hannemann wrote: Now I tried the Save and Load routines from the DUMeter project ! They're very good .. Thanks, but that will only work for flat classes. It was never intended to walk a tree. Here's some code that mite help, but I have not made a FromXml function.

      public class HashTree: System.Collections.DictionaryBase
      {
      char del;

      public HashTree()
      {
      del = (char) 0;
      }

      public HashTree(char delimter)
      {
      del = delimter;
      }

      internal int level = 0;

      private HashTree(int level, char del):this(del)
      {
      this.level = level;
      }

      public virtual HashTree this[string key]
      {
      get
      {
      string[] tokens = key.Split(del);
      HashTree outer = this;
      foreach (string keyt in tokens)
      {
      if (!outer.Dictionary.Contains(keyt))
      outer.Dictionary.Add(keyt, new HashTree(outer.level + 1, del));
      outer = outer.Dictionary[keyt] as HashTree;
      }
      return outer;
      }
      }

      public virtual System.Collections.ICollection Keys
      {
      get
      {
      return this.Dictionary.Keys;
      }
      }

      public virtual System.Collections.ICollection Values
      {
      get
      {
      return this.Dictionary.Values;
      }
      }

      private object obj;

      public object Value
      {
      get {return obj;}
      set {obj = value;}
      }

      public string ToXml()
      {
      StringBuilder output = new StringBuilder();

        string pad = "";
        for (int i = 0; i < level; i++)
           pad += "\\t";
      
        foreach (string key in Keys)
           output.AppendFormat("{2}<{0}>\\n{1}{2}\\n",
              key, (this\[key\].Count > 0 ? this\[key\].ToXml() : (pad + "\\t" + this\[key\].Value + "\\n")), pad);
        return output.ToString();
      

      }
      }

      And the usage is easy.

      HashTree tree = new HashTree('.');

      foreach(....)
      {
      tree
      ["rootlevel"]
      ["nextlevel"]
      ["anotherlevel"]
      .Value = "somevalue"; //dont you love funky usage?
      //or the same thing
      tree["rootlevel.nextlevel.anotherlevel"].Value = "somevalue";
      }

      string xml = tree.ToXml();

      This give you a nice tree, but I havent got around to making a load from XML method. If you can make one, I'll give you credit ;P. Cheers :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

      J 1 Reply Last reply
      0
      • L leppie

        j-hannemann wrote: Now I tried the Save and Load routines from the DUMeter project ! They're very good .. Thanks, but that will only work for flat classes. It was never intended to walk a tree. Here's some code that mite help, but I have not made a FromXml function.

        public class HashTree: System.Collections.DictionaryBase
        {
        char del;

        public HashTree()
        {
        del = (char) 0;
        }

        public HashTree(char delimter)
        {
        del = delimter;
        }

        internal int level = 0;

        private HashTree(int level, char del):this(del)
        {
        this.level = level;
        }

        public virtual HashTree this[string key]
        {
        get
        {
        string[] tokens = key.Split(del);
        HashTree outer = this;
        foreach (string keyt in tokens)
        {
        if (!outer.Dictionary.Contains(keyt))
        outer.Dictionary.Add(keyt, new HashTree(outer.level + 1, del));
        outer = outer.Dictionary[keyt] as HashTree;
        }
        return outer;
        }
        }

        public virtual System.Collections.ICollection Keys
        {
        get
        {
        return this.Dictionary.Keys;
        }
        }

        public virtual System.Collections.ICollection Values
        {
        get
        {
        return this.Dictionary.Values;
        }
        }

        private object obj;

        public object Value
        {
        get {return obj;}
        set {obj = value;}
        }

        public string ToXml()
        {
        StringBuilder output = new StringBuilder();

          string pad = "";
          for (int i = 0; i < level; i++)
             pad += "\\t";
        
          foreach (string key in Keys)
             output.AppendFormat("{2}<{0}>\\n{1}{2}\\n",
                key, (this\[key\].Count > 0 ? this\[key\].ToXml() : (pad + "\\t" + this\[key\].Value + "\\n")), pad);
          return output.ToString();
        

        }
        }

        And the usage is easy.

        HashTree tree = new HashTree('.');

        foreach(....)
        {
        tree
        ["rootlevel"]
        ["nextlevel"]
        ["anotherlevel"]
        .Value = "somevalue"; //dont you love funky usage?
        //or the same thing
        tree["rootlevel.nextlevel.anotherlevel"].Value = "somevalue";
        }

        string xml = tree.ToXml();

        This give you a nice tree, but I havent got around to making a load from XML method. If you can make one, I'll give you credit ;P. Cheers :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

        J Offline
        J Offline
        j hannemann
        wrote on last edited by
        #3

        Hello again Thanks leppie for you code but I am not that good to write a load routine for it. This is my first c# project. I've done saving now with another more simple solution. But it works fine. But I just wanted to try a little with XmlTextReader/Writer -- and again everytime I want to read the file I got an exception. Saving works perfect ... and the file looks fine... Has somebody any idea what I can do ??

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups