第二步,就是加入xml特性
注意,任何xml文档在其被处理之前最好先进行有效性验证,提供验证一般有两种重要的途径。其一就是提供dtd(文档类型定义),实际上就是让用户提供指令集,然后在xml文档加载的时候对其进行有效性分析,看是否有无效指令,简单说就是创建一个编译环境;另外一个就是提供一个所谓的schema。.其作用跟dtd完全一样,只是在表现形式上好于dtd,因为它本身也是一个xml文档。这里我采用了schema的形式,当然了,读者用兴趣也可以将其替换问相应的dtd版本。下面列出该schema的关于控件和topmenu对象的代码,如果要看该文档的详细代码,请看 “代码.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>
如果您对html比较熟悉的话,相信上面的代码即使是以前没有接触过也应该清楚了,在.xdr中,有两个对象,其一是元素,用前缀 elementtype 来定义,另外一个是该元素所对应的属性,用前缀atttibutetpye,定义完这两个对象后,就是要将它们实例化,对应的前缀分别是element 和atttibute,然后您就可以使用这两种已经实例化了的对象,就象使用html中<a>,<l>,<body>,<head>等标记一样。
为了更加清晰的说明上面的代码,我们这里举一个例子,相信读者一目了然:
<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>
创建完xml文档后,就要在程序里面读取xml文档,这里如果采用微软的vs.net平台开发的话,建议可以使用xmltextreader来读取相关信息,具体细节可以查阅msdn。
第三步:使用控件
创建完了menu控件,现在就是在.net平台下面来使用它。首先要在vs.net平台下面编译该控件,如果编译成功就可以使用它了。使用.net自定义控件和使用一般的activex控件很相似,启动vs.net平台下面的部件箱(可以直接在toolbar上右击鼠标来得到),注意我们制作是不是基于com组件,而是asp.net组件,所以从这点上它与com组件有本质的不同,通过browse按钮可以选择该控件对应的dll文件,将其添加进你的工程。下面你还要在你的程序代码中加入下面的代码(其他代码略)
using 你的名字空间 ; (a)
还应该在behind code加入该控件的声明 (b)
事实上,无论是a处的代码还是b处的说明,都可以由vs.net平台自动生成,如果使用其他平台并且不支持这种代码的自动添加的话,就可以手动添加。添加完之后,这样就可以和其他控件来进行使用了。而且在控件类代码中的所有的公有的成员变量,函数分别对应于控件的属性和方法。
这里需要指明的就是控件必须提供两个属性,那就是xmlstream和xmlfile,如果用户已经制作好了控件的xml文件(该文件描述了控件的数据),或者是已经写好了一个xml代码的话,可以把代码和文件分别传给xmlstream和xmlfile,后台程序(就是dll文件)通过调用一些xml的阅读器比如xmltextreader来读取xml数据,这样就达到了控件支持xml属性了,进而也增强了控件的移植性和通用性。
本文主要讨论了xml在.net平台下控件制作方面的应用,从上面的例子可以看出,xml在数据的整合,处理方面确实有它独特的优势,如果你的项目中涉及到数据处理和跨平台的问题,可以考虑应用xml.。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器