本人实训期间做了一个xml与treeview的互相导入,请机油们指教,
但是当xml文件导入treeview中时, 对于只有一个根节点的xml文件与treeview的互相导入,
不管里边有多少个子节点都不是问题
#regionTreeviewToXml
/// <summary>
/// 遍历树形图的第一阶层子节点
/// </summary>
/// <paramname="xmlDoc">根目录</param>
/// <paramname="root"></param>
private void TrNodeToXml(stringfilePath)//给定一个文件路径xml的
{
//将TreeView保存到XML文件中
XmlDocumentxmlDoc = new XmlDocument();
xmlDoc.LoadXml("<Menu></Menu>");
XmlNode root= xmlDoc.DocumentElement;//根目录
xmlDoc.InsertBefore(xmlDoc.CreateXmlDeclaration("1.0","utf-8", "yes"), root);
foreach(TreeNode tn in this.tvChannelList.Nodes)
{
if (tn.Nodes.Count > 0)
{
XmlElement Root = xmlDoc.CreateElement("Folder");//有子节点的添加为Folder
Root.SetAttribute("Name", tn.Text);
root.AppendChild(Root);
if (tn.Nodes.Count != 0)
{
//遍历子节点以下的Nodes
NodesToXml(xmlDoc, Root, tn,filePath);
}
}
}
}
/// <summary>
/// 第二子节点以后的节点保存到xml中
/// </summary>
/// <paramname="xmlDoc"></param>
/// <paramname="root"></param>
/// <paramname="tn"></param>
private void NodesToXml(XmlDocumentxmlDoc, XmlNode root, TreeNode tn,string file)
{
foreach(TreeNode temp in tn.Nodes)
{
if (temp.Nodes.Count > 0)
{
XmlElement Root = xmlDoc.CreateElement("Folder");//有子节点的添加为Folder
Root.SetAttribute("Name", temp.Text);//设置属性
root.AppendChild(Root);
if (temp.Nodes.Count != 0)
{
NodesToXml(xmlDoc, Root, temp,file);
}
}
else
{
XmlNode book = xmlDoc.CreateElement("Channel");//没有子节点的添加为Channel
XmlElement title = xmlDoc.CreateElement("name");//添加name元素
title.InnerText = temp.Text;
//title.Value = temp.Tag.ToString();
book.AppendChild(title);
XmlElement isbn = xmlDoc.CreateElement("Link");//添加link元素
if (temp.Tag == null)
{
isbn.InnerText = "";
}
else
{
isbn.InnerText = temp.Tag.ToString();
}
book.AppendChild(isbn);
root.AppendChild(book);//添加book节点到root
}
}
xmlDoc.Save(file);
}
#endregion
#region XmlToTreeview
public void LoadPara(string file)//给定一个xml的文件路径
{
XmlDocumentmyXmlDocument = new XmlDocument();
String sAppIni = file;
try
{
myXmlDocument.Load(sAppIni);
}
catch
{
MessageBox.Show("请检查文件是否存在: " + sAppIni);
return;
}
XmlElement root = myXmlDocument.DocumentElement;//获取xml文档的根目录
XmlNodeList xmlNodelist = root.ChildNodes;
foreach (XmlNode x in xmlNodelist)//循环遍历子节点
{
if (x.Name == "Folder")//如果该子节点为文件夹
{
TreeNode tn = new TreeNode();
tn.Text = x.Attributes[0].Value;
//MessageBox.Show(x.Attributes[0].Value);
this.tvChannelList.Nodes.Add(tn);
XmlNodeList temp = x.ChildNodes;//获取该文件夹下的子节点
if (temp.Count != 0)
{
getChildNode(tn, temp);//再次循环遍历下一子节点
}
}
else if (x.Name == "Channel")//如果该子节点为频道
{
TreeNode tn = new TreeNode();
tn.Text = x.FirstChild.InnerText;
tn.Tag = x.LastChild.InnerText;
this.tvChannelList.Nodes.Add(tn);
}
}
}
private void getChildNode(TreeNode treeNode, XmlNodeList nodeList)
{
foreach(XmlNode x in nodeList)//循环遍历子节点
{
if (x.Name == "Folder")//如果该子节点为文件夹
{
TreeNode tn = new TreeNode();
tn.Text = x.Attributes[0].Value;
//MessageBox.Show(x.Attributes[0].Value);
treeNode.Nodes.Add(tn);
XmlNodeList temp = x.ChildNodes;//获取该文件夹下的子节点
if (temp.Count != 0)
{
getChildNode(tn, temp);//再次循环遍历下一子节点
}
}
else if (x.Name == "Channel")//如果该子节点为频道
{
TreeNode tn = new TreeNode();
tn.Text = x.FirstChild.InnerText;
tn.Tag = x.LastChild.InnerText;
treeNode.Nodes.Add(tn);
}
}
}
#endregion
作者:大鹏