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