在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, CodeGeneration選項
{
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)];
, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
}
public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions optionUnit, 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 (!(contextis); 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-Namespace && any1.Namespace == "urn:schemas-microsoftsoft-com:xml- diffgram-v1")
{
string typeName = typeof(DataTable).FullName;
importedTypes.Add(type, typeName);
mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataTable).Namespace));
compileUnit.ReferencedAssemblies.Add(dystemlies.Add. ");
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=b77a5c561934e08
<section name="schemaImporterExtensions" type="System.Xml.Serialization.Configuration.SchemaImporterExtensionsSection, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e>
<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" requiremission="falquire/mission="falquire/mission="falquire>
</sectionGroup>
<system.xml.serialization>
<schemaImporterExtensions>
<add name="dataTableSchemaImporterExtension" type="Xrinehart.Tools.WebService.SchemaImporter.DataTableSchemaImporterExtension, Xrinehart.Tools.WebService.SchemaImporter,Version=1.0.0.0,Culture=nekenral,PublicemaImporter,Version=1.0.0.0,Culture=ne
</schemaImporterExtensions>
</system.xml.serialization>
完成以上的步驟後,再編譯WebService,重新引用(或更新Web引用),就可以正確的辨識DataTable類型了。
其實DataTable只實現了序列化,但WebService並不能自己反序列化為可辨識的格式,所以需要自己手動增加。由此可以衍生為各種業務實體BussinessEntity類別物件也可以透過以上方式實現直接傳遞。
希望對大家有幫助。
http://www.cnblogs.com/Xrinehart/archive/2006/08/20/481956.html