From the exchange data in complex B2B transactions to provide a configuration file structure for the application, XML is very big in many ways! Since XML is constantly obtaining software support, we can fully expect: XML applications will continue to increase. This article describes such an application that uses XML to create a hierarchical menu system similar to the Windows Start menu, providing a more satisfactory web experience to end users.
This app will use C #, XML, and Server-side Microsoft .NET Framework, create a DHTML structure, IE4 or higher browser to operate it and dynamically displayed on the client. Since we can quickly access the XML on the server, the XML can describe the relationship between the hierarchy, so it becomes the best choice for tag "Father / Sub Menu" data.
(A three-layer menu system, you can click to enlarge)
In addition to learning how to create a menu application using XML, we also introduce the main XML classes of the .NET framework, which is located in the System.xml collection.
What is .NET Collection (Assembly)
To use XML in the C # file, you must reference a specific namespace. One namespace in the .NET platform is used as an organizational system for a program component, which is important to solve the naming conflict, this is very like the namespace in XML. This XML-based menu system is created with the System.xml namespace located in the System.xml collection of SYSTEM.XML. .NET SDK is a collection of definitions: A collection is a collection of collection information of the type and resource, these classes and resources are used together, forming a logical unit, a "logical" DLL.
A collection requires many physical files, such as interfaces, classes, resource files, etc., and created metadata about how files work together. Version and security information may also be included in the collection. There are many benefits of the collection, one is to use in the ASP.NET application without having to add a class identification number (CLSID) to the registration file with Regsvr32.exe. In this way, the collection of upgrades is as simple as the bin directory that copies the appropriate collection to a ASP.NET application. Now let's take a closer look at the classes built in the System.xml namespace and collection.
If you have previously used Microsoft's MSXML3 digests, you will find that the classes in the SYSTEM.XML collection are quite simple. The menu application here uses only part of these major collection classes: XMLNode, XMLDocument, XMLNodeList, XMLNameDNodeMap, Documentnavigator, XMLTextReader, and XMLTextWriter.
XMLDocument, XMLNodeList, and XMLNode classes are used to create a structure that passes the menu application that passes to the client browser. Load and decompose local or remote XML documents from the server in a secure thread-safe manner in a safe thread-safe. The XML tag established in a string can also be loaded or decomposed to create, mobile nodes, or cancel the movement of the node in one document. The XMLNodeList class allows us to list a collection of nodes to access a specific properties such as name, value, or namespace. Finally, the XMLNode class can be used to assign a specific node to an XMLNode object for verification in the XML document.
The XMLNameDNodeMap class is used to enumerate the collection of properties in one element type selection. Documentnavigator, XMLTextReader, and XMLTextWriter classes provide additional features you need to use with XML. It is to be specifically described that Documentnavigator can be used to perform an XPath query that is included in the XSLT conversion. The XMLTextReader class provides only Forward-Only, unabled access to the XML node, so that the XML node also takes effect on large XML documents. The XMLTextWriter class provides a quick, forward pointer model that implements the XML content to a stream or a file. The XML document used in the menu application is relatively small, so we can use Document Object Mode (DOM) to access different nodes in the XML document on the server side. When you break down a large XML document, you need to use only the forward model contained in the XMLTextReader class.
XML code
Menu applications use 3 XML documents: MenuItems.xml, MenuItems2.xml, and MenuItems3.xml, they are used to mark separate menu items. The XML processing and operation is done by a collection called Xmlmenus.dll, which is used by a server-side ASP.NET file CreateMenus.aspx. On the client's dynamic HTML (DHTML) content uses a laminated format table file and a JavaScript file, which is combined together with the graphic results of the start section of this article.
we are at
The code of List 1 shows part of the XML documentation used to mark a separate menu item. The main element in the document is named MenuItem, which can contain a name and hyperlink element as well as additional MenuiteM child elements. This relationship can be used to create a menu system that contains submenus, just like Windows's start menu.
Since XML has already marked a layered relationship, you can walk between different elements: When you create a child node using XMLDocument, XMLNode, and XMLNodelist classes, we can repeatedly call the WalkTree () function to reverse the father / sub-relation. Other classes such as ArrayList and StreamWriter are used to classify the relevant menu into an array, and then write the generated menu structure into a file at the appropriate time.
The XMlmenus collection code begins with an XMLHierMenu namespace, followed by reference system, system.xml, system.collections, and system.io namespace:
Using system;
USING SYSTEM.XML;
Using system.collections;
Using system.io;
3 constructors are created after this part of the code. A constructor does not receive a variable, which is only initialized. The next constructor receives a custom path for an image file. The last constructor allows the output generated by the set generated to a file without not often refreshing the XML menu. The files generated by the last constructor can be static in an ASP.NET file instead of being generated at any time when requested in each web page.
After the constructor, the CREATEMENU () method begins, please see
The code in List 2.
This method is responsible for the loading and parsing of the XML document, finds a root node, and then cycles between the child nodes of the root. If a child node itself has a subtock, call the walktree () method and passes these sub-nodes therebetween. If these child nodes have subtots, WalkTree () is called again. This process loops until no additional child nodes are disconnected. The code of the WalkTree () method can be found
List 3.
At the same time that the WalkTree () method is called and the different nodes are analyzed, the MenuItem node is parsed, and the data from its hyperlink and the name node is placed in the array list. After the entire XML document is parsed, the contents of the array list are passed the page of the ASP.NET called and then written with the Response object. From this time, the client's JavaScript code starts to control the DHTML menu. On the server, we use an ASP.NET page to start the creation process of the menu. This page enters the namespace associated with the collection XMLHierMenu:
<% @ Page language = "c #"%>
<% @ Import namespace = "xmlhiermenu"%>
Then, put the style sheet file and the JavaScript file 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 walktree () method mentioned above.
List 4.
The C # code found in the ASP.NET page is just a path to different XML documents and exemplified the XMLMenu class. Once the class is illustrated, the CreateMenu () method is called. This method receives the menu name and the file path of the XML document to this menu. In this example, you want to create a menu named Menu1, Menu2, and Menu3 and then use it in an ASP.NET page. This application can support infrequent menus in a given page, but we recommend that the number of menus should not be too much, because each of the files sent to the client will increase every menu.
Compile C # file
Now we have seen the structure of the menu application, and discuss which switches can be compiled into a collection of C # files. In the .NET SDK document, we can query a complete list of all compilation switches.
To properly compile C # files to make it available in an ASP.NET page, the compiler must know that the SYSTEM.XML collection is included. This can be done by using the / R switch and adds the full path to the set to the set. Since the DLL to be created is a library, you must specify the / T switch so that the compiler will no longer look for a static main () method. The last required switch is / out, which will tell the compiler's output file name and where to put it.
All calls to the C # compiler are starting with a CSC (C # compiler) and then specify the appropriate switch. The last part of the compiler syntax includes the path to the created .cs file. Please see the full syntax format of the following compilation 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 command tells the compiler that contains the System.xml collection, compiles the file as a library to the bin directory, and enter the file name XMLmenus.cs, the output file is called Xmlmenus.dll. When the Enter key is entered, the .cs file is compiled, the generated DLL will be placed in the appropriate folder.
Conclude
By explaining an application's ideas and implementing code, we have a good understanding of some of the collections and classes in the Microsoft .NET platform. With further learning, we will see that there will be some other sets and classes to work with local and remote XML documents in a variety of ways.