xml2menu
代码
xml2treeviewmenu
新建menu.xml文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<folder title='folder 1a' >
<record title='record 1a1' />
<record title='record 1a2' />
<folder title='folder 1b'>
<record title='record 1b1' />
</folder>
</folder>
<folder title='folder 2a' >
<record title='record 2a1' />
</folder>
<folder title='folder 3a' >
<record title='record 3a1' />
<record title='record 3a2' />
</folder>
</root>
把TreeView控件命名为:treeViewMenu
在后台添加命名空间
using System.Xml;
using System.Xml.XPath;
private XmlDocument docXML = new XmlDocument();
docXML.Load("menu.xml");
populateBaseNodes();
private void populateBaseNodes()
{
treeViewMenu.Nodes.Clear();
treeViewMenu.BeginUpdate();
TreeNode treenode;
treenod = treeViewMenu.Nodes.Add("Folders");
XmlNodeList baseNodeList = docXML.SelectNodes("root\folder");
foreach(XmlNodeList xmlnode in baseNodeList)
{
string title = xmlnode.Attribute["title"].value;
treenode = treeviewMenu.Nodes.Add(title);
populateChildNodes(xmlnode, treenode);//子节点
}
treeviewMenu.EndUpdate();
treeviewMenu.Refresh();
}
private void populateChildNodes(xmlNode oldXmlnode, TreeNode oldTreenode)
{
TreeNode treenode = null;
XmlNodeList childNodeList = oldXmlnode.ChildNodes;
foreach(xmlNode xmlnode in childNodeList)
{
string title = xmlnode.Attribute["title"].Value;
treenode = oldTreenode.Nodes.Add(title);
populateChildNodes(xmlnode, treenode);
}
}
新建menu.xml文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<folder title='folder 1a' >
<record title='record 1a1' />
<record title='record 1a2' />
<folder title='folder 1b'>
<record title='record 1b1' />
</folder>
</folder>
<folder title='folder 2a' >
<record title='record 2a1' />
</folder>
<folder title='folder 3a' >
<record title='record 3a1' />
<record title='record 3a2' />
</folder>
</root>
把TreeView控件命名为:treeViewMenu
在后台添加命名空间
using System.Xml;
using System.Xml.XPath;
private XmlDocument docXML = new XmlDocument();
docXML.Load("menu.xml");
populateBaseNodes();
private void populateBaseNodes()
{
treeViewMenu.Nodes.Clear();
treeViewMenu.BeginUpdate();
TreeNode treenode;
treenod = treeViewMenu.Nodes.Add("Folders");
XmlNodeList baseNodeList = docXML.SelectNodes("root\folder");
foreach(XmlNodeList xmlnode in baseNodeList)
{
string title = xmlnode.Attribute["title"].value;
treenode = treeviewMenu.Nodes.Add(title);
populateChildNodes(xmlnode, treenode);//子节点
}
treeviewMenu.EndUpdate();
treeviewMenu.Refresh();
}
private void populateChildNodes(xmlNode oldXmlnode, TreeNode oldTreenode)
{
TreeNode treenode = null;
XmlNodeList childNodeList = oldXmlnode.ChildNodes;
foreach(xmlNode xmlnode in childNodeList)
{
string title = xmlnode.Attribute["title"].Value;
treenode = oldTreenode.Nodes.Add(title);
populateChildNodes(xmlnode, treenode);
}
}