手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

ASP.NET 2.0 WebService中传递DataTable参考

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

  在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" />

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!