设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

ASP.NET 2.0 WebService中传递DataTable参考

作者:佚名 出处:网络转载 时间:08-24 点击:

内容载入中...
华腾联合陪你一路学习 upschool.com.cn 在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;

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口