Use C # and XML to create a dynamic hierarchical menu

Source: Internet
Author: User

From Exchanging data in complex B2B transactions to providing configuration file structures for applications, XML is a great tool in many aspects! As XML is continuously supported by software, we can fully predict that XML applications will continue to grow. This article introduces an application that uses XML to create a hierarchical menu system similar to a Windows Start menu, so as to provide end users with a better Web experience.

This application uses the C #, XML, and server-side Microsoft. Net Framework to create a DHTML structure. IE4 or a later browser can operate on it and dynamically display it on the client. Because we can quickly access the XML on the server and the XML can describe the relationship between layers, it is the best choice to mark "Parent/sub menu" data.

(A three-tier menu system that can be clicked to enlarge)

In addition to learning how to use XML to create a menu application, we also introduce the main XML classes of the. Net Framework, which are located in the System. Xml collection.

What Is A. Net set (Assembly)

To use XML in the C # file, you must reference a specific namespace. A namespace in the. Net platform is used as an organizational system for program components. It is very important for resolving naming conflicts, which is like a namespace in XML. The XML-based menu System is created using the System. Xml namespace located in the System. Xml collection .. . Net SDK defines a set as follows: A set is a collection of types and resources. These classes and resources are used together to form a functional logical unit, that is, a "logical" dll.

A collection requires many physical files, such as interfaces, classes, and resource files, and creates metadata about how files work together. It may also contain version and security information. A collection can be used in the asp.netapplication without adding a class identification number (CLSID) to the registration file ). In this way, the collection upgrade operation is as simple as copying an appropriate collection to the bin directory of an ASP. NET application. Now let's take a closer look at the classes created in the System. Xml namespace and set.

If you have used Microsoft's MSXML3 parser before, you will find that using the class in the System. Xml set is quite simple. The menu application uses only one part of these main collection classes: XmlNode, XmlDocument, XmlNodeList, XmlNamedNodeMap, DocumentNavigator, XmlTextReader, and XmlTextWriter.

XmlDocument, XmlNodeList, and XmlNode classes are used to create the structure of the menu application passed to the client browser. The XmlDocument class is used to load and break down local or remote XML documents from the server in a thread-safe way. XML tags created in a string can also be loaded or decomposed to create, move, or cancel node movement in a document. The XmlNodeList class allows us to list a set of nodes to access a specific attribute, such as name, value, or namespace. Finally, the XmlNode class can be used to assign a specific node to an XmlNode object used for validation in the XML document.

The XmlNamedNodeMap class is used to list attribute sets created in an element type selection. The DocumentNavigator, XmlTextReader, and XmlTextWriter classes provide the additional functions required for use with XML. Specifically, DocumentNavigator can be used to execute XPath queries, which are included in XSLT transformations. The XmlTextReader class provides forward-only and cache-free access to XML nodes, so that XML nodes can also take effect for large XML documents. The XmlTextWriter class provides a fast and forward-only pointer model for writing XML content to a stream or 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 breaking down large XML documents, you need to use only the forward model contained in the XmlTextReader class.

XML Code

The menu application uses three XML documents: menuItems. xml, menuItems2.xml, and menuItems3.xml, which are used to mark separate menu items. XML processing and operations are completed by a collection named xmlMenus. dll, which is used by a server-side ASP. NET file createMenus. aspx. The Dynamic HTML (DHTML) content on the client uses a stacked table file and a JavaScript file. These files are combined to generate the graph results in the beginning of this article.

The code in List 1 shows part of the XML document used to mark individual menu items. The main element in the document is named menuItem. It can contain a name, hyperlink element, and additional menuItem sub-element. This relationship can be used to create a menu system that contains sub-menus, just like a Windows Start Menu.

Because XML has marked a hierarchical relationship, we can use loops to walk between different elements: when using the XmlDocument, XmlNode, and XmlNodeList classes to create subnodes, we can call the hierarchical tree () repeatedly () function to reverse the parent/child relationship. Other classes such as ArrayList and StreamWriter are used to classify related menus into arrays, and then write the generated menu structure into a file at an appropriate time.

When the code of the XmlMenus collection starts, you must declare an XmlHierMenu namespace. Next, you must reference System, System. Xml, System. Collections, and System. IO namespace:

Using System;

Using System. Xml;

Using System. Collections;

Using System. IO;

Create three constructors after this part of the code. A constructor only initializes variables without receiving variables. The next constructor receives the custom path of an image file. When the last constructor does not frequently refresh the XML menu, the output generated by the set can be stored in a file. The file generated by the last constructor can be statically included in an ASP. NET file, rather than generated at any time when each Web page request is made.

After the constructor, start to define the CreateMenu () method. For details, see the code in List 2.

This method loads and parses XML documents, finds the root node, and then loops between the root nodes. If a subnode itself has a subnode, call the tree () method and pass these subnodes to it. If these subnodes have subnodes, call the Spanning Tree () method again (). This process repeats until no more child nodes are found. For the code of the tree () method, see List 3.

While calling the tree () method and analyzing different nodes, you must parse the menuItem node and put the data from its Hyperlink and Name nodes in the array list. After the entire XML file is parsed, the content of the array list is passed back to the ASP. NET page called, and then the information is written using the Response object. From then on, the client's JavaScript code began to control the DHTML menu.

On the server side, we use an ASP. NET page to start the menu creation process. On this page, enter the namespace XmlHierMenu associated with the set:

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

<% @ Import Namespace = "XmlHierMenu" %>

Then, the style sheet file and JavaScript file are included in the Code area of the file. Finally, the Code at the bottom of the ASP. NET page is used to reference the CreateMenu () method and decision tree () method mentioned above. See List 4.

The C # code found on the ASP. NET page only establishes paths to different XML documents and provides examples for the XmlMenu class. Once this class is used as an example, the CreateMenu () method is called. This method receives the menu name and the file path of the XML document to the menu. In this example, three menus named menu1, menu2, and menu3 are created and used on an ASP. NET page. This application can support an infinite number of menus on a given page. However, we recommend that you do not add too many menus, because each time you add a menu, the file size sent to the client increases.

Compile C # files

Now we have seen the structure of the menu application. Next we will discuss which switches can be used to compile the C # file into a collection. In the. Net SDK documentation, We can query a complete list of all compilation switches.

To compile the C # file correctly so that it can be used on an ASP. NET page, the compiler must know that it contains the System. Xml set. This can be done by using the/r switch and adding the complete path of the set to the end. Since the dll to 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 required switch is/out, which will tell the compiler what the output file name is and where it is put.

All calls to the C # compiler start with csc (C # compiler) and specify the appropriate switch. The last part of the compiler syntax includes the path to the created. cs file. See the complete syntax format of the following compilation file:

C:> csc/r: System. Xml. dll/

T: library/out: d: inetpubwwwroot

XmlinxmlMenus. dll d: inetpub

WwwrootxmlmenusxmlMenus. cs

This command tells the compiler to include System. xml set, compile the file as a library, output the file to the bin directory, and name the input file xmlMenus. cs, the output file name is xmlMenus. dll. After you enter the Enter key, the. cs file will be compiled and the generated dll will be placed in the appropriate folder.

Conclusion

By explaining the idea and implementation code of an application, this article gives us a good understanding of some collections and classes in Microsoft. Net platform. With further study, we will see that there are other collections and classes that can work with local and remote XML documents in multiple ways.


Related Article

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.