1

我有一个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>

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

在此处输入图像描述

当前输出为

在此处输入图像描述

到目前为止我的程序

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

1 回答 1

1

一个建议,你的 else 部分可以替换为

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

更新的 XML:

概括了所需的结构。您的 xml 的设计与您期望的 UI(treeView)不同步。我的意思是您希望完成的父子关系。因此,xml 需要进行一些小的修改,例如添加父SqlEyeWarnings& 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
    }

最终输出:

在此处输入图像描述

于 2013-06-06T04:16:21.870 回答