网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.开发手记之实现web.config的快速.
.asp.net错误处理封装.
.asp.net中一次更新DATAGRID中所有.
.ASP.NET中备份SQL Server数据库的.
.ASP.NET2.0实现无刷新客户端回调.
.那数组存入application,再利用循.
.当DataSet中包含主/子表时,Updat.
.ASP.NET 2.0 中使用自定义缓存依.
..Net中如何操作IIS(原理篇).
.asp.net连接Access数据库.
.Asp.net cache 简述.
.对asp.net1.1开发模板类的一些修.
.DotNet中用到的加密算法总结.
.[DataGird]如何截取过长的字符串.
..net下访问Access数据库需要注意.
.安装好.net之后如何运行asp.net程.
.UNIX时间戳与.net日期类的转换 .
.脚本获取选中文字及所在句子.
.asp.net 操作xml.
.ASP 2.0 数据绑定函数Eval()的机.

ASP.NET 2.0 WebService中传递DataTable参考

发表日期:2006-8-21


  在2.0正式版发布之前,就满天的看到关于DataTable支持序列化的新特性宣传,满以为从此以后使用DataTable就和DataSet一样方便了,结果在应用项目的时候才发现并非那么回事。
  DataTable是支持序列化了,但是微软并没有把他做的特别方便,还需要我们自己来做一些工作之后才能够在WebService里面传递DataTable,否则在引用DataTable的时候会发现DataTable变成了一个什么Proxy类型。
  首先编写类DataTableSchemaImporterExtension,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization.Advanced;
using System.Collections;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Xml;
using System.Data;

namespace Xrinehart.Tools.WebService.SchemaImporter
{
    class DataTableSchemaImporterExtension : SchemaImporterExtension
    {

        // DataTableSchemaImporterExtension is used for WebServices, it is used to recognize the schema for DataTable within wsdl

        Hashtable importedTypes = new Hashtable();

 

        public override string ImportSchemaType(string name, string schemaNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
        {

            IList values = schemas.GetSchemas(schemaNamespace);

            if (values.Count != 1)
            {

                return null;

            }

            XmlSchema schema = values[0] as XmlSchema;

            if (schema == null)

                return null;

            XmlSchemaType type = (XmlSchemaType)schema.SchemaTypes[new XmlQualifiedName(name, schemaNamespace)];

            return ImportSchemaType(type, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);

        }

 

        public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
        {

            if (type == null)
            {

                return null;

            }

            if (importedTypes[type] != null)
            {

                mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));

                compileUnit.ReferencedAssemblies.Add("System.Data.dll");

                return (string)importedTypes[type];

            }

            if (!(context is XmlSchemaElement))

                return null;

 

            if (type is XmlSchemaComplexType)
            {

                XmlSchemaComplexType ct = (XmlSchemaComplexType)type;

                if (ct.Particle is XmlSchemaSequence)
                {

                    XmlSchemaObjectCollection items = ((XmlSchemaSequence)ct.Particle).Items;

                    if (items.Count == 2 && items[0] is XmlSchemaAny && items[1] is XmlSchemaAny)
                    {

                        XmlSchemaAny any0 = (XmlSchemaAny)items[0];

                        XmlSchemaAny any1 = (XmlSchemaAny)items[1];

                        if (any0.Namespace == XmlSchema.Namespace && any1.Namespace == "urn:schemas-microsoft-com:xml-diffgram-v1")
                        {

                            string typeName = typeof(DataTable).FullName;

                            importedTypes.Add(type, typeName);

                            mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataTable).Namespace));

                            compileUnit.ReferencedAssemblies.Add("System.Data.dll");

                            return typeName;

                        }

                    }

                }

            }

            return null;

        }

    }


}
  为此类添加进一个项目中,并将此项目进行强命名后编译。

  然后,把该Assembly程序集加入到GAC中。

  最后修改本机的machine.config,代码如下:
      <sectionGroup name="system.xml.serialization" type="System.Xml.Serialization.Configuration.SerializationSectionGroup, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="schemaImporterExtensions" type="System.Xml.Serialization.Configuration.SchemaImporterExtensionsSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <section name="dateTimeSerialization" type="System.Xml.Serialization.Configuration.DateTimeSerializationSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <section name="xmlSerializer" type="System.Xml.Serialization.Configuration.XmlSerializerSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </sectionGroup>
 
 
 
 
  <system.xml.serialization>
     <schemaImporterExtensions>
            <add name="dataTableSchemaImporterExtension" type="Xrinehart.Tools.WebService.SchemaImporter.DataTableSchemaImporterExtension, Xrinehart.Tools.WebService.SchemaImporter,Version=1.0.0.0,Culture=neutral,PublicKeyToken=5a627ce15fb94702" />
    </schemaImporterExtensions>
 </system.xml.serialization>

  完成以上的步骤后,再编译WebService,重新引用(或者更新Web引用),就可以正确的识别DataTable类型了。
  其实DataTable只实现了序列化,但WebService并不能自己反序列化为可识别的格式,所以需要自己手动增加。由此可以衍生为各种业务实体BussinessEntity类对象也可以通过以上方式实现直接传递。

  希望对大家有所帮助。

http://www.cnblogs.com/Xrinehart/archive/2006/08/20/481956.html

上一篇:ASP.NET实现下拉框二级联动组件 人气:7638
下一篇:ASP.NET服务器控件PleaseWaitButton[翻译] 人气:5241
浏览全部ASP.NET 2.0的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐