无法在Treeview中正确填充

时间:2013-06-06 03:46:16

标签: c# treeview

我有一个xml文件,如

<?xml version="1.0" encoding="utf-8"?>
<ScriptFileNames>
  <SqlEye>
    <SqlEyeWarnings Name="SN006: Function name should start with fn. ">
      <File Name="dbo.ParseStringList.UserDefinedFunction.sql" />
    </SqlEyeWarnings>
    <SqlEyeWarnings Name="SD030:  object does not exist in database or is invalid for this operation in Database">
      <File Name="dbo.SQLEyeLookUp_InsertReservedWordsScript.script.sql" />
    </SqlEyeWarnings>
    <SqlEyeWarnings Name="SD004: Check for existence object then Drop statement before create statement">
      <File Name="_ws_CallLogs_DeleteAll.sql" />
      <File Name="_ws_CallLogs_GetCallIdByUserId.sql" />
      <File Name="_ws_CallLogs_InsertReconciled.sql" />
      <File Name="_ws_CallLogs_P_DeleteByUserIdCallId.sql" />
      <File Name="_ws_CommandHistory_AllHistory.sql" />
    </SqlEyeWarnings>
    <SqlEyeRemarks Name="SD007: Missing create index statement.">
      <File Name="dbo.CachedPlan.table.sql" />
    </SqlEyeRemarks>
    <SqlEyeRemarks Name="SD009: Missing or order mismatch of Grant statement.">
      <File Name="dbo.ParseStringList.UserDefinedFunction.sql" />
    </SqlEyeRemarks>
    <SqlEyeRemarks Name="SD001: Set QuotedIdentifier ON statement is missing or order mismatch or it should be ON.">
      <File Name="_ws_CallLogs_DeleteAll.sql" />
      <File Name="_ws_CallLogs_GetCallIdByUserId.sql" />
      <File Name="_ws_CallLogs_InsertReconciled.sql" />
      <File Name="_ws_CallLogs_P_DeleteByUserIdCallId.sql" />
      <File Name="_ws_CommandHistory_AllHistory.sql" />
    </SqlEyeRemarks>
  </SqlEye>
</ScriptFileNames>

我想在树视图中填充它,使输出看起来像

enter image description here

当前输出

enter image description here

我的计划到目前为止

private void populateTreeview()
{
    try
        {
            TextReader textReader = new StringReader("test.xml");

            //First, we'll load the Xml document
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(textReader);

            //Now, clear out the treeview, and add the first (root) node
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = (TreeNode)treeView1.Nodes[0];

            //We make a call to AddNode, where we'll add all of our nodes
            addTreeNode(xDoc.DocumentElement, tNode);

            //Expand the treeview to show all nodes
            treeView1.ExpandAll();

        }
        catch(XmlException xExc) //Exception is thrown is there is an error in the Xml
        {
            MessageBox.Show(xExc.Message);
        }
        catch(Exception ex) //General exception
        {
            MessageBox.Show(ex.Message);
        }
}

string fileName = "";
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
    XmlNode xNode;
    TreeNode tNode;
    XmlNodeList xNodeList;

    if (xmlNode.HasChildNodes) //The current node has children
    {
        xNodeList = xmlNode.ChildNodes;

        for (int x = 0; x <= xNodeList.Count - 1; x++) //Loop through the child nodes
        {
            xNode = xmlNode.ChildNodes[x];
            treeNode.Nodes.Add(new TreeNode(xNode.Name));
            tNode = treeNode.Nodes[x];
            addTreeNode(xNode, tNode);
        }
    }
    else //No children, so add the outer xml (trimming off whitespace)
        treeNode.Text = xmlNode.OuterXml.Trim()
            .Replace("<File Name=", string.Empty)
            .Replace("/>", string.Empty)
            .Replace("\"", string.Empty);
}

1 个答案:

答案 0 :(得分:1)

建议,您的其他部分可以替换为

treeNode.Text = xmlNode.Attributes.GetNamedItem("Name").Value;

更新了XML:

概括了所需的结构。你的xml并不是与你期望的UI(treeView)同步设计的。我的意思是你想要完成的父子关系。因此,xml需要一些小修改,比如添加一个父母SqlEyeWarnings&amp; SqlEyeRemarks。我使用属性Name作为display Name/Text

的占位符
<?xml version="1.0" encoding="utf-8"?>
<ScriptFileNames Name="ScriptFileNames">
  <SqlEye Name="SqlEye">
        <SqlEyeWarnings Name="SqlEyeWarnings">
            <SqlEyeWarning Name="SN006: Function name should start with fn. ">
              <File Name="dbo.ParseStringList.UserDefinedFunction.sql" />
            </SqlEyeWarning>
            <SqlEyeWarning Name="SD030:  object does not exist in database or is invalid for this operation in Database">
              <File Name="dbo.SQLEyeLookUp_InsertReservedWordsScript.script.sql" />
            </SqlEyeWarning>
            <SqlEyeWarning Name="SD004: Check for existence object then Drop statement before create statement">
              <File Name="_ws_CallLogs_DeleteAll.sql" />
              <File Name="_ws_CallLogs_GetCallIdByUserId.sql" />
              <File Name="_ws_CallLogs_InsertReconciled.sql" />
              <File Name="_ws_CallLogs_P_DeleteByUserIdCallId.sql" />
              <File Name="_ws_CommandHistory_AllHistory.sql" />
            </SqlEyeWarning>
        </SqlEyeWarnings>
        <SqlEyeRemarks Name="SqlEyeRemarks">
            <SqlEyeRemark Name="SD007: Missing create index statement.">
              <File Name="dbo.CachedPlan.table.sql" />
            </SqlEyeRemark>
            <SqlEyeRemark Name="SD009: Missing or order mismatch of Grant statement.">
              <File Name="dbo.ParseStringList.UserDefinedFunction.sql" />
            </SqlEyeRemark>
            <SqlEyeRemark Name="SD001: Set QuotedIdentifier ON statement is missing or order mismatch or it should be ON.">
              <File Name="_ws_CallLogs_DeleteAll.sql" />
              <File Name="_ws_CallLogs_GetCallIdByUserId.sql" />
              <File Name="_ws_CallLogs_InsertReconciled.sql" />
              <File Name="_ws_CallLogs_P_DeleteByUserIdCallId.sql" />
              <File Name="_ws_CommandHistory_AllHistory.sql" />
            </SqlEyeRemark>
        </SqlEyeRemarks>
  </SqlEye>
</ScriptFileNames>

代码中的次要更新:

           //First, we'll load the Xml document
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(@"D:\test.xml");

    private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
    {
        XmlNode xNode;
        TreeNode tNode;
        XmlNodeList xNodeList;

        if (xmlNode.HasChildNodes) 
        {
            xNodeList = xmlNode.ChildNodes;

            for (int x = 0; x <= xNodeList.Count - 1; x++) 
            {
                xNode = xmlNode.ChildNodes[x];
                treeNode.Nodes.Add(new TreeNode(xNode.Attributes.GetNamedItem("Name").Value)); //New Update
                tNode = treeNode.Nodes[x];
                addTreeNode(xNode, tNode);
            }
        }
        else //No children, so add the outer xml (trimming off whitespace)
            treeNode.Text = xmlNode.Attributes.GetNamedItem("Name").Value; //New Update
    }

最终输出:

enter image description here