博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web Service返回DataTable(zz)
阅读量:7111 次
发布时间:2019-06-28

本文共 3427 字,大约阅读时间需要 11 分钟。

找了好久才找到的:)
 

DataTable implements IXmlSerializable, and the schema for the DataTable is unique enough allowing wsdl.exe to take advantage of the new feature (SchemaImporterExtension) to generate DataTable on the client.

 

 Here is what you need to do

  1. Create SchemaImporterExtension that will recognize the DataSetSchema:

The V2 Framework uses anonymous complexType for DataSet schema:

None.gif
<
s:complexType
>
None.gif    
<
s:sequence
>
None.gif      
<
s:any minOccurs
=
"
0
"
 maxOccurs
=
"
unbounded
"
 
namespace
=
"
http://www.w3.org/2001/XMLSchema
"
 processContents
=
"
lax
"
 
/>
None.gif      
<
s:any minOccurs
=
"
1
"
 
namespace
=
"
urn:schemas-microsoft-com:xml-diffgram-v1
"
 processContents
=
"
lax
"
 
/>
None.gif    
</
s:sequence
>
None.gif
</
s:complexType
>
None.gif

You need to write the extension that maps the above schema pattern to a DataTable type:

None.gif
class
 DataTableSchemaImporterExtension : SchemaImporterExtension
ExpandedBlockStart.gif
{
InBlock.gif    
//
 DataTableSchemaImporterExtension is used for WebServices, it is used to recognize the schema for DataTable within wsdl
InBlock.gif
    Hashtable importedTypes 
=
 
new
 Hashtable();
InBlock.gif
InBlock.gif    
public
 
override
 
string
 ImportSchemaType(
string
 name, 
string
 schemaNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
ExpandedSubBlockStart.gif    
{
InBlock.gif        IList values 
=
 schemas.GetSchemas(schemaNamespace);
InBlock.gif        
if
 (values.Count 
!=
 
1
)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return
 
null
;
ExpandedSubBlockEnd.gif        }
InBlock.gif        XmlSchema schema 
=
 values[
0
as
 XmlSchema;
InBlock.gif        
if
 (schema 
==
 
null
)
InBlock.gif            
return
 
null
;
InBlock.gif        XmlSchemaType type 
=
 (XmlSchemaType)schema.SchemaTypes[
new
 XmlQualifiedName(name, schemaNamespace)];
InBlock.gif        
return
 ImportSchemaType(type, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    
public
 
override
 
string
 ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
if
 (type 
==
 
null
)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return
 
null
;
ExpandedSubBlockEnd.gif        }
InBlock.gif        
if
 (importedTypes[type] 
!=
 
null
)
ExpandedSubBlockStart.gif        
{
InBlock.gif            mainNamespace.Imports.Add(
new
 CodeNamespaceImport(
typeof
(DataSet).Namespace));
InBlock.gif           compileUnit.ReferencedAssemblies.Add(
"
System.Data.dll
"
);
InBlock.gif            
return
 (
string
)importedTypes[type];
ExpandedSubBlockEnd.gif        }
InBlock.gif        
if
 (
!
(context 
is
 XmlSchemaElement))
InBlock.gif            
return
 
null
;
InBlock.gif        
if
 (type 
is
 XmlSchemaComplexType)
ExpandedSubBlockStart.gif        
{
InBlock.gif            XmlSchemaComplexType ct 
=
 (XmlSchemaComplexType)type;
InBlock.gif            
if
 (ct.Particle 
is
 XmlSchemaSequence)
ExpandedSubBlockStart.gif            
{
InBlock.gif                XmlSchemaObjectCollection items 
=
 ((XmlSchemaSequence)ct.Particle).Items;
InBlock.gif                
if
 (items.Count 
==
 
2
 
&&
 items[
0
is
 XmlSchemaAny 
&&
 items[
1
is
 XmlSchemaAny)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    XmlSchemaAny any0 
=
 (XmlSchemaAny)items[
0
];
InBlock.gif                    XmlSchemaAny any1 
=
 (XmlSchemaAny)items[
1
];
InBlock.gif                    
if
 (any0.Namespace 
==
 XmlSchema.Namespace 
&&
 any1.Namespace 
==
 
"
urn:schemas-microsoft-com:xml-diffgram-v1
"
)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        
string
 typeName 
=
 
typeof
(DataTable).FullName;
InBlock.gif                        importedTypes.Add(type, typeName);
InBlock.gif                      mainNamespace.Imports.Add(
new
 CodeNamespaceImport(
typeof
(DataTable).Namespace));
InBlock.gif           compileUnit.ReferencedAssemblies.Add(
"
System.Data.dll
"
);
InBlock.gif                        
return
 typeName;
ExpandedSubBlockEnd.gif                    }
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        
return
 
null
;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

 

  1. Compile and GAC the SchemaImporterExtension
  2. Add it to the existent extensions in machine.config, ysing fully-qualified assembly name
    None.gif<system.xml.serialization>
    None.gif   
    <schemaImporterExtensions>
    None.gif        
    <add name="DataTableSchemaImporterExtension" type="DataTableSchemaImporterExtension,dot.gif        </schemaImporterExtensions>
    None.gif
    </system.xml.serialization>
    None.gif

  本文转自Silent Void博客园博客,原文链接:http://www.cnblogs.com/happyhippy/archive/2007/05/23/756860.html,如需转载请自行联系原作者

你可能感兴趣的文章
法国政府宣布数字行业发展战略:3D打印和物联网
查看>>
基于Hadoop的云盘系统客户端技术难点之一 上传和下载效率优化
查看>>
JSP的运行原理
查看>>
12篇学通C#网络编程——第二篇 HTTP应用编程(上)
查看>>
讨喜的隔离可变性-前言
查看>>
iOS开发CoreAnimation解读之二——对CALayer的分析
查看>>
IOS UISwitch 用法总结
查看>>
使用UIWebView中html标签显示富文本
查看>>
iOS图片压缩上传
查看>>
Velocity官方指南-Velocity是如何工作的
查看>>
network block device(nbd)
查看>>
keepalived instance priority don't impact by track script|interface weight when 255
查看>>
Apache Storm 官方文档 —— 配置
查看>>
SASS学习参考
查看>>
Vue初学知识点记录(一)
查看>>
Android性能优化之内存泄漏处理(图文)
查看>>
[译]如何Mock Express的会话
查看>>
基于豆瓣api的快应用项目
查看>>
前端状态管理简易实现(以vuex为例)
查看>>
SVG和Vector的概念和如何在Android Studio中使用
查看>>