游客:
注册
|
登录
|
帮助
网友俱乐部
»
网络编程
»
XML教程
» XML在.net平台下的自定义控件的应用(2)
网友生活网
|
网友学堂
如何获得金币?
诚招斑竹
IP归属地查询 PR查询 收录查询 whois查询
免费超短2级域名,your.jpy.cc
传世私服
|
传奇世界私服
注册亚洲交友中心,找个人来同居吧!
免费存储空间,免费网络硬盘
‹‹ 上一主题
|
下一主题 ››
投票
交易
悬赏
活动
打印
|
推荐
|
订阅
|
收藏
标题: XML在.net平台下的自定义控件的应用(2)
问天
元帅
UID 11493
精华
187
积分 34201
帖子 33353
威望 91
金币 13148
热心 2617
阅读权限 100
注册 2006-4-7
状态 离线
#1
使用道具
发表于 2007-2-20 11:36
资料
个人空间
主页
短消息
加为好友
XML在.net平台下的自定义控件的应用(2)
&;#58853;第二步,就是加入XML特性
&;#58853;&;#58853;注意,任何xml文档在其被处理之前最好先进行有效性验证,提供验证一般有两种重要的途径。其一就是提供DTD(文档类型定义),实际上就是让用户提供指令集,然后在xml文档加载的时候对其进行有效性分析,看是否有无效指令,简单说就是创建一个编译环境;另外一个就是提供一个所谓的schema。.其作用跟DTD完全一样,只是在表现形式上好于DTD,因为它本身也是一个XML文档。这里我采用了schema的形式,当然了,读者用兴趣也可以将其替换问相应的DTD版本。下面列出该schema的关于控件和topmenu对象的代码,如果要看该文档的详细代码,请看 “代码.doc”。
&;#58853;&;#58853;menu.xdr:
&;#58853;&;#58853;<?xml version="1.0" encoding="UTF-8"?>
&;#58853;&;#58853;<Schema name="menus" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
&;#58853;&;#58853;<description>
&;#58853;&;#58853;menus schema used to validate menu.xml
&;#58853;&;#58853;</description>
&;#58853;&;#58853;<ElementType name="menu" model="closed" content="empty">
&;#58853;&;#58853;<AttributeType name="id" dt:type="string" required="yes"/>
&;#58853;&;#58853;<atttibute type="id"/>
&;#58853;&;#58853;<element name="topmenu" model="closed" minOccurs="0" maxOccurs="*" content="empty"/>
&;#58853;&;#58853;</ElementType>
&;#58853;&;#58853;<ElementType name="topmenu" model="closed" content="empty">
&;#58853;&;#58853;<AttributeType name="id" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="text" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="linkurl" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="isparent" dt:type="string" required="yes"/>
&;#58853;&;#58853;<atttibute type="id"/>
&;#58853;&;#58853;<atttibute type="text"/>
&;#58853;&;#58853;<atttibute type="linkurl"/>
&;#58853;&;#58853;<atttibute type="isparent"/>
&;#58853;&;#58853;<element name="submenu" model="closed" minOccurs="0" maxOccurs="*" content="empty"/>
&;#58853;&;#58853;</ElementType>
&;#58853;&;#58853;<ElementType name="submenu" model="closed" content="empty">
&;#58853;&;#58853;<AttributeType name="id" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="text" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="linkurl" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="isparent" dt:type="string" required="yes"/>
&;#58853;&;#58853;<atttibute type="id"/>
&;#58853;&;#58853;<atttibute type="text"/>
&;#58853;&;#58853;<atttibute type="linkurl"/>
&;#58853;&;#58853;<atttibute type="isparent"/>
&;#58853;&;#58853;<element name="childrenmenu" model="closed" minOccurs="0" maxOccurs="*" content="empty"/>
&;#58853;&;#58853;</ElementType>
&;#58853;&;#58853;<ElementType name="childrenmenu" model="closed" content="empty">
&;#58853;&;#58853;<AttributeType name="id" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="text" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="linkurl" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="isparent" dt:type="string" required="yes"/>
&;#58853;&;#58853;<atttibute type="id"/>
&;#58853;&;#58853;<atttibute type="text"/>
&;#58853;&;#58853;<atttibute type="linkurl"/>
&;#58853;&;#58853;<atttibute type="isparent"/>
&;#58853;&;#58853;<element name="menuitem" model="closed" minOccurs="0" maxOccurs="*" content="empty"/>
&;#58853;&;#58853;</ElementType>
&;#58853;&;#58853;<ElementType name="menuitem" model="closed" content="empty">
&;#58853;&;#58853;<AttributeType name="id" dt:type="string" required="yes"/>
&;#58853;&;#58853;<AttributeType name="text" dt:type="string" required="yes"/>
&;#58853;&;#58853;<atttibute type="id"/>
&;#58853;&;#58853;<atttibute type="text"/>
&;#58853;&;#58853;</ElementType>
&;#58853;&;#58853;</Schema>
&;#58853;&;#58853;如果您对html比较熟悉的话,相信上面的代码即使是以前没有接触过也应该清楚了,在.xdr中,有两个对象,其一是元素,用前缀 ElementType 来定义,另外一个是该元素所对应的属性,用前缀atttibuteTpye,定义完这两个对象后,就是要将它们实例化,对应的前缀分别是Element 和atttibute,然后您就可以使用这两种已经实例化了的对象,就象使用html中<a>,<l>,<body>,<head>等标记一样。
&;#58853;&;#58853;为了更加清晰的说明上面的代码,我们这里举一个例子,相信读者一目了然:
&;#58853;&;#58853;<menus xmlns='x-schema:Menu.xdr'>
&;#58853;&;#58853;<menu id='menuBar1'>
&;#58853;&;#58853;<topmenu id="xjtu" text="xjtu" isparent="true" linkurl="xjtu"></topmenu>
&;#58853;&;#58853;<submenu id="sina" text="sina" isparent="false" linkurl="sina"></submenu>
&;#58853;&;#58853;<topmenu id="sohu" text="sohu" isparent="false" linkurl="sohu"></topmenu>
&;#58853;&;#58853;</menu></menus>
&;#58853;&;#58853;创建完xml文档后,就要在程序里面读取xml文档,这里如果采用微软的vs.net平台开发的话,建议可以使用XmlTextReader来读取相关信息,具体细节可以查阅MSDN。
&;#58853;&;#58853;第三步:使用控件
&;#58853;&;#58853;创建完了menu控件,现在就是在.net平台下面来使用它。首先要在vs.net平台下面编译该控件,如果编译成功就可以使用它了。使用.net自定义控件和使用一般的ActiveX控件很相似,启动vs.net平台下面的部件箱(可以直接在ToolBar上右击鼠标来得到),注意我们制作是不是基于COM组件,而是ASP.net组件,所以从这点上它与COM组件有本质的不同,通过Browse按钮可以选择该控件对应的dll文件,将其添加进你的工程。下面你还要在你的程序代码中加入下面的代码(其他代码略)
&;#58853;&;#58853;using 你的名字空间 ; (A)
&;#58853;&;#58853;还应该在behind code加入该控件的声明 (B)
&;#58853;&;#58853;事实上,无论是A处的代码还是B处的说明,都可以由VS.NET平台自动生成,如果使用其他平台并且不支持这种代码的自动添加的话,就可以手动添加。添加完之后,这样就可以和其他控件来进行使用了。而且在控件类代码中的所有的公有的成员变量,函数分别对应于控件的属性和方法。
&;#58853;&;#58853;这里需要指明的就是控件必须提供两个属性,那就是XmlStream和XmlFile,如果用户已经制作好了控件的XML文件(该文件描述了控件的数据),或者是已经写好了一个xml代码的话,可以把代码和文件分别传给XmlStream和XmlFile,后台程序(就是dll文件)通过调用一些XML的阅读器比如XmlTextReader来读取xml数据,这样就达到了控件支持xml属性了,进而也增强了控件的移植性和通用性。
&;#58853;&;#58853;本文主要讨论了xml在.net平台下控件制作方面的应用,从上面的例子可以看出,xml在数据的整合,处理方面确实有它独特的优势,如果你的项目中涉及到数据处理和跨平台的问题,可以考虑应用XML。
网友
问天
签名 - 网友社区
请您回个帖。谢谢
PR查询
免费域名
免费空间
[广告]
免费域名(Free Subdomain)
免费空间(Free hosting)
PR查询(Google Pagerank)
投票
交易
悬赏
活动
控制面板首页
编辑个人资料
积分交易
公众用户组
好友列表
个人空间管理
基本概况
流量统计
客户软件
发帖量记录
论坛排行
主题排行
发帖排行
积分排行
在线时间
管理团队
管理统计
当前时区 GMT+8, 现在时间是 2008-10-12 08:42
信产部ICP备案:
京ICP备05066424号
北京市公安局网监备案:1101050648号
Powered by
Discuz!
5.5.0
TOP
清除 Cookies
-
联系我们
-
网友俱乐部
-
Archiver
-
WAP