Before the official version 2.0 was released, I saw all the publicity about the new features of DataTable supporting serialization. I thought that using DataTable would be as convenient as DataSet from now on. However, I found out that this was not the case when I applied the project.
DataTable supports serialization, but Microsoft has not made it particularly convenient. We still need to do some work ourselves before we can pass the DataTable in the WebService. Otherwise, when we reference the DataTable, we will find that the DataTable has become a proxy. type.
First write the class DataTableSchemaImporterExtension, the code is as follows:
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
;
, 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)
{
{
XmlSchemaAny any0 = (XmlSchemaAny)items[0];
XmlSchemaAny any1 = (XmlSchemaAny)items[1];
if (any0.Namespace == 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;
}
}
}
Add this class to a project, and compile the project after strongly naming it.
Then, add the Assembly assembly to the GAC.
Finally modify the local machine.config, the code is as follows:
<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>
After completing the above steps, compile the WebService and re-reference (or update the Web reference), you can correctly identify the DataTable type.
In fact, DataTable only implements serialization, but WebService cannot deserialize it into a recognizable format by itself, so it needs to be added manually. From this, various business entities can be derived. BusinessEntity class objects can also be directly transferred through the above methods.
Hope it helps everyone.
http://www.cnblogs.com/Xrinehart/archive/2006/08/20/481956.html