The application of XML to the custom control under the. NET Platform

Source: Internet
Author: User
Tags empty functions net variables string xml attribute xmlns
xml| Control

XML full name Extensible Markup Language (extensible marked language), which has been popular all over the world in recent years due to its unique advantages in data processing, cross-platform and so on. The XML language system handles any data as "keys" and "values", similar to many database management systems (DBMS), and is independent of specific machine instructions, and is stored in plain text files, and thus has excellent cross-platform performance. In addition, it allows users to create their own data instruction system, in the framework of this instruction system, users can transform all the information into easy to access data, greatly accelerated the development of the cycle and code portability.

Since XML is so good, what does it look like? Recently, the author has developed a menu control based on the. NET platform and added XML attribute because of the need of the subject. The following steps are made:

Step One: Create a Menu control under the. NET Platform

The custom controls below the. NET platform work as follows: The control corresponds to a class, and all properties of the control are exposed through the class's member variables, and the control's methods correspond to the member functions of class. In the hand that the control is loaded, the system calls the class's Render method, and when making the control, it should overload (override) The Render method so that the control can "draw" itself at the moment the control is loaded, in general, In the Render method, you put those JavaScript files that are drawn into the control interface into the output stream.

Since it is important here to explain the application of XML later, here is just a brief description of the original custom control, the following list of related objects, attributes, methods.

The entire menu control is divided into Topmenu,submenu,childrenmenu,menuitem four objects, and the corresponding information is:

Control properties:

ID: Identifying the name of the control

Menubarheight: The height of the menu stop

Menubarwidth:: The width of the menu bar

BorderWidth:: Border width

Autodropdown:true for automatic Drop-down, false table is mouse click dropdown

Dropdowndelay: Drop down time

Menufont: Fonts

Locationx: The x position of the menu

Locationy: The y position of the menu

Xmlfile:xml file

Xmlstream:xml Flow

Topmenu Object Properties:

ID: Ibid.

Text: Top-level menu texts

Isparent:true indicates that there is a next level menu, otherwise false (false default)

Islink: True is required for event handling, otherwise false (false default);

bgcolor: Background color

Forecolor: Foreground color

Hoveredbackcolor: The color of the mouse over the top

Topmenufont: Fonts

Linkurl: When Isparent is False, this indicates the URL of the connection

Borderwidth:topmenu Border Width

ItemHeight: The height of the next level menu (must be isparent to be true)

Itemwidth: The width of the next level menu (must be isparent to be valid)

Submenus: List of arrays containing the next level of menus

All the attributes of Submenu,childrenmenu and MenuItem are the same as the topmenu above.
The second step is to add XML features

Note that any XML document is best validated before it is processed, and there are generally two important ways to provide validation. The first is to provide a DTD (document type definition), in fact, let the user provide the instruction set, and then in the XML document when loading the validity analysis, to see whether there are invalid instructions, simply to create a compilation environment; the other is to provide a so-called schema. It works exactly like a DTD. , it is better than a DTD in its presentation, because it is an XML document itself. Here I take the form of a schema, and of course, the reader can replace it with interest by asking the appropriate DTD version. The following lists the schema's code for controls and Topmenu objects, and if you want to see the detailed code for that document, see Code. doc.

MENU.XDR:

<?xml version= "1.0" encoding= "UTF-8"?

<schema name= "Menus" xmlns= "Urn:schemas-microsoft-com:xml-data" xmlns:dt= "Urn:schemas-microsoft-com:datatypes"

<description>

Menus schema used to validate Menu.xml

</description>

<elementtype name= "menu" model= "closed" content= "empty"

<attributetype name= "id" dt:type= "string" required= "Yes"/>

<atttibute type= "id"/>

<element name= "Topmenu" model= "closed" minoccurs= "0" maxoccurs= "*" content= "empty"/>

</ElementType>



<elementtype name= "Topmenu" model= "closed" content= "empty"

<attributetype name= "id" dt:type= "string" required= "Yes"/>

<attributetype name= "Text" dt:type= "string" required= "Yes"/>

<attributetype name= "Linkurl" dt:type= "string" required= "Yes"/>

<attributetype name= "Isparent" dt:type= "string" required= "Yes"/>

<atttibute type= "id"/>

<atttibute type= "text"/>

<atttibute type= "Linkurl"/>

<atttibute type= "Isparent"/>

<element name= "submenu" model= "closed" minoccurs= "0" maxoccurs= "*" content= "empty"/>

</ElementType>



<elementtype name= "submenu" model= "closed" content= "empty"

<attributetype name= "id" dt:type= "string" required= "Yes"/>

<attributetype name= "Text" dt:type= "string" required= "Yes"/>

<attributetype name= "Linkurl" dt:type= "string" required= "Yes"/>

<attributetype name= "Isparent" dt:type= "string" required= "Yes"/>

<atttibute type= "id"/>

<atttibute type= "text"/>

<atttibute type= "Linkurl"/>

<atttibute type= "Isparent"/>

<element name= "Childrenmenu" model= "closed" minoccurs= "0" maxoccurs= "*" content= "empty"/>

</ElementType>



<elementtype name= "Childrenmenu" model= "closed" content= "empty"

<attributetype name= "id" dt:type= "string" required= "Yes"/>

<attributetype name= "Text" dt:type= "string" required= "Yes"/>

<attributetype name= "Linkurl" dt:type= "string" required= "Yes"/>

<attributetype name= "Isparent" dt:type= "string" required= "Yes"/>

<atttibute type= "id"/>

<atttibute type= "text"/>

<atttibute type= "Linkurl"/>

<atttibute type= "Isparent"/>

<element name= "MenuItem" model= "closed" minoccurs= "0" maxoccurs= "*" content= "empty"/>

</ElementType>

<elementtype name= "MenuItem" model= "closed" content= "empty"

<attributetype name= "id" dt:type= "string" required= "Yes"/>

<attributetype name= "Text" dt:type= "string" required= "Yes"/>

<atttibute type= "id"/>

<atttibute type= "text"/>

</ElementType>

</Schema>

If you are familiar with HTML, it should be clear that the above code, even if it has not been contacted before, in. XDR, there are two objects, one of which is the element, defined by the prefix ElementType, and the other is the corresponding attribute of the element, prefixed with a atttibutetpye, After the two objects are defined, they are instantiated, with the corresponding prefixes of element and atttibute, and then you can use both of the objects that have already been instantiated, as in HTML <a>, <l>, <body>,
To get a clearer picture of the code above, let's take an example and trust the reader at a glance:

<menus xmlns= ' x-schema:menu.xdr '

<menu id= ' menuBar1 '

<topmenu id= "Xjtu" text= "Xjtu" isparent= "true" Linkurl= "Xjtu" > </topmenu>

<submenu id= "Sina" text= "Sina" isparent= "false" Linkurl= "Sina" > </submenu>

<topmenu id= "Sohu" text= "Sohu" isparent= "false" Linkurl= "Sohu" > </topmenu>

</menu> </menus>


After you create the XML document, you must read the XML document in the program, and if you are using Microsoft's Vs.net platform, it is recommended that you use XmlTextReader to read the relevant information, which can be consulted on MSDN.

Step three: Use the control

The menu control is created and is now used under the. NET Platform. You first compile the control under the Vs.net platform, and you can use it if the compilation succeeds. Using a. NET custom control is similar to using a generic ActiveX control. Start the widget box underneath the Vs.net platform (you can right-click on the toolbar to get it), note that we are making a COM component instead of a asp.net component, so from this point it is essentially different from the COM component, through B The Rowse button can select the corresponding DLL file for the control and add it to your project. Below you also need to add the following code in your program code (other code slightly)

Using your name space; (A)

You should also add the declaration of the control in behind code (B)

In fact, both the code at a and the description of B can be automatically generated by the Vs.net platform, and can be added manually if you use another platform and do not support the automatic addition of this code. After you've added it, you can use it with other controls. And all the public member variables in the control class code, functions correspond to the properties and methods of the control.

What needs to be specified here is that the control must provide two properties, that is Xmlstream and xmlfile, if the user has made the control's XML file (which describes the control's data), or has already written an XML code, You can pass code and files to Xmlstream and xmlfile, and the daemon (that is, the DLL file) reads XML data by invoking some XML readers such as XmlTextReader, which enables the control to support XML properties. It also enhances the portability and versatility of the control.

This paper mainly discusses the application of XML in control making in. NET platform, and from the above example, we can see that XML has its unique advantages in data integration and processing, if your project involves data processing and cross-platform problems, you can consider applying XML.



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.