Recently, because of a small tool, you need to read the data from the XML format configuration, and dynamically generate the menu ... because it is a rookie, it is a little bit of mind .. I have an open spirit, although it is just a little, but I believe There is still a little help for novices. Now take it out and share it .. If there is any opinion, welcome advice: appledgedotnet@hotmail.com
1. Require data from the XML file and generate a menu, and process it according to the relevant data when clicking on the menu; 2, the example XML and the picture are seen. 3, program code, and note: a) First, a DataMenuItem (ie, the menu containing data) is derived from MenuItem, because there may be a lot of data in the XML source file to save, and MenuItem does not save the data PUBLIC CLASS DATAMENUITEM: MenuItem {? Public string [] attributenames, attributevalues; // Save all attribute names and values in the source XML node of this menu? Public string value; // The value of the node (value this method may not be quite accurate, it should be ? InnerText) public string GetAttributeValueByName (string AttributeName) // get a way for the future of the program remaining property values {?? if (attributeNames == null || AttributeValues == null) return "";? ?? if (attributeNames. Length! = Attributevalues.length) Return ""; ?? INT i; ?? for (i = 0; i
?? {
??? f (attributenames [i] == attributename) Return AttributeValues [i];
??}
?? Return ""
?
}
b) The next method of adding XML data to a menu, detailed explanation Private void XmlTomenu (XMLNode Node, MenuItem MENU) {? Int i ;? DataMenuItem TempMenuItem = New DataMenuItem (); TempMenuItem.index = menu.Menuitems .Count; // should be omitted.? TempMenuItem.text = node.name;? TempMenuItem.Value = node.innerText;? If (node.attributes.count> 0) // If there is a property, it is saved. No. No. No The property skips? {?? TempMenuItem.attributenames = new string [node.attributes.count]; ?? TempMenuItem.attributevalues = new string [node.attributes.count]; ?? for (i = 0; i
?? {
??? TempMenuItem.attributenames [i] = node.attributes [i] .name;
??? TempMenuItem.attributevalues [i] = node.attributes [i] .value;
??}
?
TempMenuItem.Click = new system.eventhandler (this.DataMenu_Click); // How to add a process of handling the Click event, since DataMenuItem is data, this processing method can be different from different processing depending on the data.
Menu.MenuItems.Add (TempMenuItem);
? if (node.childnodes.count> 1) // Because there is no child node, node.childnodes.count will be 1? {
?? foreach (xmlnode nodes in node.childnodes)
?? {
??? xmltomenu (nodes, menu.menuitems [menu.menuitems.count-1]); // Recurns call itself
??}
?
}
c) Method for handling Click events:
Private void DataMenu_click (Object Sender, System.Eventargs E)
{
DataMenuItem Clicked = (DataMenuItem) Sender;
MessageBox.show (Clicked.Value); // This is just testing, and you can use the data in DataMenuItem according to the different situation;
}
d) Finally, you can only call the above method. Provide the following code for reference, some of which are more easy to understand, more often, and spend some steps;
Menu_Xml.MenuItems.Clear (); // Menu_Xml can modify; int i; XmlDocument xmldoc = new XmlDocument (); XmlTextReader reader = new XmlTextReader ( "Ini.xml"); xmldoc.Load (reader); XmlNodeList NodeList = xmldoc .Documentelement.childNodes; for (i = 0; i
{
XMLNode Node = NodeList [i];
? Xmltomenu (Node, Menu_Xml);
}
Reader.Close ();
?
?
Example xml: ini.xml
1
2
3
4
?
1
?
2
?
3
?
??
1
??
2
?