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 に答える