Recently, because of a small tool, you need to read data from the XML format configuration and dynamically generate menus... because it was a cainiao, I had to worry about it .. although the spirit of openness is just a small thing, I believe it will be helpful to new users. share it with you now .. if you have any comments, welcome advice: AppleDotnet@hotmail.com
1. Requirement: read data from the XML file, generate a menu, and process the data according to the relevant data when you click the menu;
2. See the XML and image examples.
3. program code and comments:
A) First, derive a datamenuitem from menuitem (that is, the menu containing data), because the XML source file may have a lot of data to save, and menuitem does not store the data.
Public class datamenuitem: menuitem
{
? Public String [] attributenames, attributevalues; // save all attribute names and values in the source XML node of the menu.
? Public String Value; // The value of the node (the value may not be accurate, it should be innertext)
? Public String getattributevaluebyname (string attributename) // a method for obtaining attribute values for Future Programs
? {
?? If (attributenames = NULL | attributevalues = NULL) Return "";
?? If (attributenames. length! = Attributevalues. Length) Return "";
?? Int I;
?? For (I = 0; I?? {
??? If (attributenames [I] = attributename) return attributevalues [I];
??}
?? Return "";
?}
}
B) then there is a method to add XML data to the menu. For details, see the comment.
Private void xmltomenu (xmlnode node, menuitem menu)
{
? Int I;
? Datamenuitem tempmenuitem = new datamenuitem ();
? Tempmenuitem. Index = menu. menuitems. Count; // It should be omitted.
? Tempmenuitem. Text = node. Name;
? Tempmenuitem. value = node. innertext;
? If (node. Attributes. Count> 0) // If an attribute exists, it is saved. If no attribute exists, it is skipped.
? {
?? 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); // Add a method to process the click event. Because datamenuitem contains data, this method can be processed differently based on data.
? Menu. menuitems. Add (tempmenuitem );
? If (node. childnodes. Count> 1) // because node. childnodes. Count is 1 even if no subnode exists.
? {
?? Foreach (xmlnode nodes in node. childnodes)
?? {
??? Xmltomenu (nodes, menu. menuitems [menu. menuitems. Count-1]); // recursively calls itself
??}
?}
}
C) how to handle the click event:
Private void datamenu_click (Object sender, system. eventargs E)
{
? Datamenuitem clicked = (datamenuitem) sender;
? MessageBox. Show (clicked. Value); // This is just a test. You can use the data in datamenuitem as needed;
}
D) Finally, you only need to call the above method. The following code is provided for reference. In some cases, it takes more steps to make it easier to understand;
Menu_xml.menuitems.clear (); // menu_xml can be modified by yourself;
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
?