Prior to the release of the 2.0 official release, the new features of the DataTable support serialization were seen all over the sky, and it was thought that using the DataTable from then on would be as convenient as the dataset, and the result would be that it was not the case when the project was applied.
The DataTable supports serialization, but Microsoft does not make it particularly convenient, and it requires us to do some work before we can pass the DataTable in WebService. Otherwise, when referencing the DataTable, you will find that the DataTable becomes a type of proxy.
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 are used to recognize the schema for DataTable within WSDL
Hashtable importedtypes = new Hashtable ();
public override string Importschematype (string name, String schemanamespace, XmlSchemaObject context, XmlSchema s 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, Xmlsch Emaimporter Importer, CodeCompileUnit CompileUnit, CodeNamespace mainnamespace, codegenerationoptions options, CodeDomProvider Codeprovider)
{
if (type = = null)
{
return null;
}
if (Importedtypes[type]!= null)
{
MainNamespace.Imports.Add (new CodeNamespaceImport (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 (DataTable). Namespace));
compileUnit.ReferencedAssemblies.Add ("System.Data.dll");
return typeName;
}
}
}
}
return null;
}
}
}