ASP.NET 2.0 WebService中传递DataTable - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- ASP.NET 2.0 WebService中传递DataTable (http://www.webasp.net/article/28/27802.htm) |
| -- 作者:未知 -- 发布日期: 2006-09-20 |
|
在2.0正式版发布之前,就满天的看到关于DataTable支持序列化的新特性宣传,满以为从此以后使用DataTable就和DataSet一样方便了,结果在应用项目的时候才发现并非那么回事。 namespace Xrinehart.Tools.WebService.SchemaImporter // 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,代码如下: 完成以上的步骤后,再编译WebService,重新引用(或者更新Web引用),就可以正确的识别DataTable类型了。 |
| webasp.net |