I have an utility in C# that parses source code and creates an xml report of all misspelled words in quoted literal strings, the file and location in the file where they appear. I have another utility that reads this xml file and loads it into a treeview which looks like the following layout:
MisspelledWords
|
|___badd
| |__Suggestions
| | |__bad
| |
| |__Locations
| |__Location
| |__FileName
| | |__ C:\Workspaces\MyProject\Project1\program.cs
| |
| |__LineNumber
| | |
| | |_ 31
| |
| |__Original Line
|
|___spellling
| |__Suggestions
| | |__spelling
| |
| |__Locations
| |__Location
| |__FileName
| | |__ C:\Workspaces\MyProject\Project1\program.cs
| |
| |__LineNumber
| | |
| | |_ 55
| |
| |__Original Line
Everything loads into the treeview successfully sorted the first time, however when I clear the treeview, reload it and sort (treeview1.sort), all of the child nodes are added to the last misspelled word node on level 1.
Here is a snippet of my current code to load and sort.
private void button1_Click(object sender, EventArgs e)
{
if (!bTreeLoaded)
{
//Add the "Ignored" Top Level node.
TreeNode ignoreNode = new TreeNode("Ignored List");
treeView1.Nodes.Add(ignoreNode);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(textBox1.Text);
TreeNode misspelledWordsNode = new TreeNode(xmldoc.DocumentElement.Name);
treeView1.Nodes.Add(misspelledWordsNode);
AddNode(xmldoc.DocumentElement, misspelledWordsNode);
treeView1.Sort();
bTreeLoaded = true;
}
else
{
MessageBox.Show("Data has already been loaded");
}
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
private void button2_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
bTreeLoaded = false;
}
My XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<MisspelledWords xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<badd>
<Suggestions>
<Suggestion>bad</Suggestion>
</Suggestions>
<Locations>
<Location>
<FileName>C:\Workspaces\AHLTA\Current\VB6\global.bas</FileName>
<LineNumber>31</LineNumber>
<OriginalLine>s = "badd spellling"</OriginalLine>
</Location>
</Locations>
</badd>
</MisspelledWords>