本人自己的测试,进行循环遍历config文件信息
private void loadXml(string filePath)
{
XmlDocument document = new XmlDocument();
document.Load(filePath);
XmlNode root = document.DocumentElement;
textBox1.AppendText("<" + root.Name + ">\r\n");
BianliXml(root);
textBox1.AppendText("</" + root.Name + ">\r\n");
}
private void BianliXml(XmlNode node)
{
XmlNodeList nodeList = node.ChildNodes;
//nodeList = root.SelectNodes("configSections/sectionGroup");//获取appSettings节点的所有子节点
foreach (XmlNode add in nodeList)//遍历所有了节点
{
XmlAttributeCollection ac = add.Attributes;
if (ac!=null && ac.Count > 0)
{
string str = "<" + add.Name + " ";
for (int i = 0; i < ac.Count; i++)
{
str = str + ac[i].Name + "=\"" + ac[i].Value + "\" ";
}
textBox1.AppendText(str + "/>\r\n");//listBox1类型是ListBox
if (add.ChildNodes.Count > 0)
{
textBox1.AppendText("<" + add.Name + ">\r\n");//listBox1类型是ListBox
BianliXml(add);
textBox1.AppendText("</" + add.Name + ">\r\n");//listBox1类型是ListBox
}
}else if(add.Name=="#text")
{
textBox1.AppendText(add.Value);//listBox1类型是ListBox
}else if(add.Name=="#comment")
{
continue;
}
else
{
textBox1.AppendText("<" + add.Name + ">\r\n");//listBox1类型是ListBox
BianliXml(add);
textBox1.AppendText("</" + add.Name + ">\r\n");//listBox1类型是ListBox
}
}
}