度假Treeview的拼写错误的单词

时间:2015-01-05 13:15:19

标签: c# xml treeview

我在C#中有一个实用程序,用于解析源代码,并在引用的文字字符串中创建所有拼写错误的单词的xml报告,文件中的文件和位置出现在文件中。我有另一个实用程序读取此xml文件并将其加载到树视图中,该视图类似于以下布局:

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 

第一次加载到树视图中的所有内容都成功排序,但是当我清除树视图时,重新加载它并排序(treeview1.sort),所有子节点都被添加到级别1上的最后一个拼写错误的单词节点。 p>

以下是我当前加载和排序代码的片段。

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;
}

我的XML文件如下所示:

<?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>

1 个答案:

答案 0 :(得分:0)

我无法重现您的问题,因为您不会在重新加载之前显示如何清除预先存在的节点树。在显示的代码中,您只是不允许在树中重新加载节点。

通常,如果要将树更新为某些分层数据,则需要合并每个级别的列表,并使用其名称或标记键入节点。例如,以下内容似乎适用于您的XML:

    private void LoadXML_Click(object sender, EventArgs e)
    {
        treeView1.BeginUpdate();
        try
        {
            //Add the "Ignored" Top Level node. 
            if (!treeView1.Nodes.ContainsKey("Ignored List"))
                treeView1.Nodes.Add(new TreeNode { Name = "Ignored List", Text = "Ignored List" });

            var xmldoc = GetXmlDocument(); // From your UI
            int changed = AddOrMergeNodes(xmldoc);
            Debug.WriteLine("Added or removed " + changed + " nodes");

            if (changed > 0)
            {
                treeView1.ExpandAll();
                treeView1.Sort();
            }
        }
        finally
        {
            treeView1.EndUpdate();
        }

        treeView1.Focus();
    }

    private void clearXML_Click(object sender, EventArgs e)
    {
        treeView1.Nodes.Clear();
    }

    static Dictionary<string, List<TreeNode>> ToNodeDictionary(TreeNodeCollection nodes)
    {
        return nodes.Cast<TreeNode>().Aggregate(new Dictionary<string, List<TreeNode>>(), (d, n) => { d.Add(n.Name, n); return d; });
    }

    int AddOrMergeNodes(XmlDocument xmldoc)
    {
        var dict = ToNodeDictionary(treeView1.Nodes);
        return AddOrMergeNode(treeView1.Nodes, dict, xmldoc.DocumentElement);
    }

    static int AddOrMergeNodes(TreeNodeCollection treeNodeCollection, XmlNodeList xmlNodeList)
    {
        int changed = 0;
        var dict = ToNodeDictionary(treeNodeCollection);
        foreach (XmlNode inXmlNode in xmlNodeList)
        {
            changed += AddOrMergeNode(treeNodeCollection, dict, inXmlNode);
        }
        foreach (var leftover in dict.Values.SelectMany(l => l))
        {
            treeNodeCollection.Remove(leftover);
            changed++;
        }
        return changed;
    }

    static int AddOrMergeNode(TreeNodeCollection treeNodeCollection, Dictionary<string, List<TreeNode>> dict, XmlNode inXmlNode)
    {
        int changed = 0;
        var name = inXmlNode.Name;

        TreeNode node;
        if (!dict.TryRemoveFirst(name, out node))
        {
            node = new TreeNode { Name = name, Text = name };
            treeNodeCollection.Add(node);
            changed++;
        }
        Debug.Assert(treeNodeCollection.Contains(node), "treeNodeCollection.Contains(node)");
        if (inXmlNode.HasChildNodes)
        {
            var text = name;
            if (node.Text != text)
                node.Text = name;
            changed += AddOrMergeNodes(node.Nodes, inXmlNode.ChildNodes);
        }
        else
        {
            var text = (inXmlNode.OuterXml).Trim();
            if (node.Text != text)
                node.Text = text;
            node.Nodes.Clear();
        }
        return changed;
    }

然后是一些处理列表字典的扩展方法,为方便起见:

public static class DictionaryExtensions
{
    public static void Add<TKey, TValueList, TValue>(this IDictionary<TKey, TValueList> listDictionary, TKey key, TValue value)
        where TValueList : IList<TValue>, new()
    {
        if (listDictionary == null)
            throw new ArgumentNullException();
        TValueList values;
        if (!listDictionary.TryGetValue(key, out values))
            listDictionary[key] = values = new TValueList();
        values.Add(value);
    }

    public static bool TryGetValue<TKey, TValueList, TValue>(this IDictionary<TKey, TValueList> listDictionary, TKey key, int index, out TValue value)
        where TValueList : IList<TValue>
    {
        TValueList list;
        if (!listDictionary.TryGetValue(key, out list))
            return Returns.False(out value);
        if (index < 0 || index >= list.Count)
            return Returns.False(out value);
        value = list[index];
        return true;
    }

    public static bool TryRemoveFirst<TKey, TValueList, TValue>(this IDictionary<TKey, TValueList> listDictionary, TKey key, out TValue value)
        where TValueList : IList<TValue>
    {
        TValueList list;
        if (!listDictionary.TryGetValue(key, out list))
            return Returns.False(out value);
        var count = list.Count;
        if (count > 0)
        {
            value = list[0];
            list.RemoveAt(0);
            if (--count == 0)
                listDictionary.Remove(key);
            return true;
        }
        else
        {
            listDictionary.Remove(key); // Error?
            return Returns.False(out value);
        }
    }
}

public static class Returns
{
    public static bool False<TValue>(out TValue value)
    {
        value = default(TValue);
        return false;
    }
}
相关问题