Creating dynamic hierarchical menus with C # and XML

Source: Internet
Author: User
Tags constructor switches thread
xml| Menu | create | Dynamically from exchanging data in complex business-to-business transactions to providing a configuration file structure for applications, XML is in many ways! As XML continues to receive software support, we can fully foresee that XML will continue to grow in application. This article describes an application that uses XML to create a layered menu system similar to the Windows Start menu, providing a more satisfying web experience for end users.

This application will use C #, XML, and server-side Microsoft. NET Framework, creating a DHTML structure that IE4 or later browsers can manipulate and dynamically display on the client. Because we can quickly access the XML on the server, and XML can describe the relationship between hierarchies, it becomes a best choice for labeling "Parent/submenu" data.


(a three-layer menu system, click to enlarge)

In addition to learning how to create a menu application using XML, we also want to introduce. NET Framework's primary XML classes, which are located in the System.Xml collection.

What is. NET collection (Assembly)
To use XML in a C # file, you must reference a specific namespace ... NET platform is used as an organization system for a program component, which is important for resolving naming conflicts, much like namespaces in XML. This xml-based menu system is created using the System.Xml namespace located in the System.Xml collection. The. Net SDK is defined as a collection: A set is a collection of types and resources that are used together to form a functional logical unit, a " Logical DLL.

A collection requires many physical files, such as interfaces, classes, resource files, and so on, and creates metadata about how files work together. The collection may also contain version and security information. Collections have many benefits, one of which is that they can be used in asp.net applications without having to add a class identification number (CLSID) to the registration file with Regsvr32.exe. In this way, the upgrade of the collection is as simple as copying the appropriate collection into the bin directory of a asp.net application. Now let's take a closer look at the classes that are built in System.Xml namespaces and collections.

If you've used a Microsoft MSXML3, you'll find that using the classes in the System.Xml collection is fairly straightforward. The menu application here uses only a subset of these main collection classes: XmlNode, XmlDocument, XmlNodeList, XmlNamedNodeMap, Documentnavigator, XmlTextReader, and XmlTextWriter.

The XmlDocument, XmlNodeList, and XmlNode classes are used to create the structure of the menu application that is passed to the client browser. The XmlDocument class loads and decomposes local or remote XML documents from the server in a thread (Thread-safe) way. XML tags built into a string can also be loaded or exploded to create, move, or cancel the movement of nodes in a document. The XmlNodeList class allows us to enumerate a collection of nodes to access a particular attribute, such as a name, value, or namespace. Finally, the XmlNode class can be used to assign a specific node to a XmlNode object for validation in an XML document.

The XmlNamedNodeMap class is used to enumerate the collection of attributes built into an element type selection. The Documentnavigator, XmlTextReader, and XmlTextWriter classes provide the extra functionality needed to work with XML. Specifically, Documentnavigator can be used to perform XPath queries, which are included in the XSLT transformation. The XmlTextReader class provides only forward (forward-only), no cache access to XML nodes, making the XML nodes effective for large XML documents as well. The XmlTextWriter class provides a fast, forward-only pointer model that implements the purpose of writing XML content to a stream or a file.

The XML document used in the menu application is relatively small, so we can use the Document Object Mode (DOM) on the server side to access different nodes in the XML document. When you decompose large XML documents, you need to use only the forward model that the XmlTextReader class contains.

XML Code
The menu application uses 3 XML documents: Menuitems.xml, Menuitems2.xml, and Menuitems3.xml, which are used to mark individual menu items. The processing and manipulation of XML is done by a collection called XmlMenus.dll, which is used by a asp.net file createmenus.aspx on the server side. Dynamic HTML (DHTML) content on the client uses a cascading format table file and a JavaScript file that is combined to produce the graphical results of the beginning of this article.

Our code in List 1 shows part of the XML document that is used to mark individual menu items. The main element in the document is named MenuItem, which can contain a name and a hyperlink element as well as an extra MenuItem child element. This relationship can be used to create a menu system that contains submenus, just like the Start menu for Windows.
Because XML has already marked hierarchical relationships, you can use loops to walk between different elements: when you create child nodes using the XmlDocument, XmlNode, and XmlNodeList classes, we can repeatedly invoke the Walktree () function to reverse the parent/child relationship. Other classes such as ArrayList and StreamWriter are used to categorize the related menus into an array and then write the resulting menu structure to a file at the appropriate time.

To declare a Xmlhiermenu namespace at the beginning of the code for the Xmlmenus collection, then refer to the System, System.Xml, System.Collections, and System.IO namespaces:

Using System;

Using System.Xml;

Using System.Collections;

Using System.IO;

After this part of the code, you create 3 constructors. A constructor does not receive a variable and only initializes the variable. The next constructor receives a custom path for an image file. The last constructor allows the output generated by the collection to be stored in a file without frequently refreshing the XML menu. The file generated by the last constructor can be statically included in an ASP. NET file, rather than at any time when the individual Web page requests are generated.

After the constructor, start defining the CreateMenu () method, and see the Code in List 2.
This method is responsible for loading and parsing the XML document, locating the root node, and then looping between the child nodes of the root. If a child node itself is found to have child nodes, the Walktree () method is invoked and the child nodes are passed to them. If these child nodes also have child nodes, call Walktree () again. This process loops until no more child nodes are found. The code for the Walktree () method can refer to list 3.
When the Walktree () method is invoked and the different nodes are parsed, the MenuItem node is parsed, and the data from its hyperlink and name node is placed in the array list. After parsing the entire XML document, the contents of the array list are passed back to the ASP.net page of the call, and the information is written out using the response object. From this point on, the client's JavaScript code starts to control the DHTML menu.

On the server side, we use an ASP. NET page to start the menu creation process. This page has entered the namespace Xmlhiermenu associated with the collection:

<%@ page language= "C #"%>

<%@ Import namespace= "Xmlhiermenu"%>

The stylesheet file and JavaScript file are then included in the code area of the file. Finally, the code at the bottom of the ASP.net page is used to refer to the CreateMenu () method mentioned above and the Walktree () method, see List 4.
The C # code found in the ASP.net page simply establishes a path to a different XML document, and an example of the Xmlmenu class is shown. Once you have an example of this class, call the CreateMenu () method. This method receives the menu name and the file path to the XML document to the menu. In this case, you create 3 menus named Menu1, MENU2, and MENU3, and then use them in a asp.net page. This application can support an infinite number of menus on a given page, but we recommend that the number of menus not be too much, because each additional menu will increase the size of the file sent to the client.

Compiling C # files
Now that we've seen the structure of the menu application, let's discuss which switches are used to compile the C # file into a collection. In the. Net SDK documentation, we can query a complete list of all the compile switches.

To properly compile a C # file so that it can be used in a asp.net page, the compiler must know that the System.Xml collection is included. This can be done by using the/R switch and then adding the full path to the collection later. Because the DLL that will be created is a library, you must specify the/T switch so that the compiler no longer looks for a static Main () method. The last switch you need is out of/out, which tells the compiler what the file name is and where to put it.

All calls to the C # compiler start with the CSC (C # compiler), and then specify the appropriate switch. The last part of the compiler syntax includes the path to the. cs file that you have created. See the full syntax format for the following compiled file:

C:\>csc/r:system.xml.dll/

T:library/out:d:\inetpub\wwwroot\

Xml\bin\xmlmenus.dll d:\inetpub\

Wwwroot\xml\menus\xmlmenus.cs

This line of command tells the compiler to include the System.Xml collection, compile the file as a library, output the file to the bin directory, name the input file XmlMenus.cs, and the output file named XmlMenus.dll. When the ENTER key is entered, the. cs file is compiled and the resulting DLL is placed in the appropriate folder.

Conclusion
This article explains the idea and implementation code of an application to enable us to use Microsoft. NET platform has a good understanding of some of the collections and classes. As we learn more, we'll see that there are other collections and classes that can work with local and remote XML documents in a variety of ways.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.