国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET 2.0 WebService中傳遞DataTable參考

2019-11-18 17:03:14
字體:
來源:轉載
供稿:網友

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

  完成以上的步驟后,再編譯WebService,重新引用(或者更新Web引用),就可以正確的識別DataTable類型了。
  其實DataTable只實現了序列化,但WebService并不能自己反序列化為可識別的格式,所以需要自己手動增加。由此可以衍生為各種業務實體BussinessEntity類對象也可以通過以上方式實現直接傳遞。

  希望對大家有所幫助。

http://m.survivalescaperooms.com/Xrinehart/archive/2006/08/20/481956.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 林州市| 乾安县| 岚皋县| 宁陕县| 丹棱县| 南漳县| 西乌| 枣庄市| 汽车| 东丽区| 太仓市| 吴江市| 武定县| 贵阳市| 鄄城县| 宜州市| 剑阁县| 沁阳市| 林西县| 通辽市| 克东县| 青铜峡市| 吴旗县| 奉贤区| 三原县| 蒙城县| 湟中县| 忻州市| 嫩江县| 闽侯县| 阳原县| 霍城县| 宜兰县| 巴南区| 钦州市| 崇礼县| 客服| 长宁区| 广昌县| 广昌县| 清流县|