Again : How to save the TreeView
-
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: -
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: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-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
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 ??