All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#教程:拖放技术的典型应用实例
每个开发人员现在应该下载的十种必备工具,2_[Asp.Net教程]
asp.net2.0中水晶报表的应用实例
C#如何让数据访问过程更透明_[Asp.Net教程]
.net下用javascript调用webservice_[Asp.Net教程]
.NET开发不可不知、不可不用的辅助类(一)_[Asp.Net教程]
开始了解,PHP,V5,中的对象_php资料_编程技术
C#网络应用编程基础练习题与答案(二)_[Asp.Net教程]
C#中创建Windows窗体
C#中的常用的数学术语
Reading number is top 10 pictures
9.3阅兵全景图5-地空反舰导弹梯队
BingBingFan apple dew point photo gallery2
Go to the national museum3
From China fortress sora aoi3
Fat bird to fly
美丽的风景--让你目瞪口呆
Small QiShu -- ShuangShuangPan1
身材野火台灣美女2
Take you to walk into the most true north Korea rural3
mythology hero1
Download software ranking
Unix video tutorial17
Boxer vs Yellow2
Tram sex maniac 2 (H) rar bag13
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
打鸟视频
matrix2
星际争霸1.08硬盘免安装版
1400篇各类破解文章
Unix video tutorial19
尖东毒玫瑰B
delv published in(发表于) 2014/1/10 6:23:46 Edit(编辑)
ASP.NET,2.0,WebService中传递DataTable参考_[Asp.Net教程]

ASP.NET,2.0,WebService中传递DataTable参考_[Asp.Net教程]

ASP.NET 2.0 WebService中传递DataTable参考_[Asp.Net教程]

在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,代码如下:










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


  希望对大家有所帮助。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.