ASP.net 2.0 WebService pass the DataTable reference

Source: Internet
Author: User

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;


     }


   }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.