Web Service返回DataTable(zz)


找了好久才找到的:)
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=154939&SiteID=1

 

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.gifContractedBlock.gif
dot.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.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        IList values 
= schemas.GetSchemas(schemaNamespace);
InBlock.gif        
if (values.Count != 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif        XmlSchema schema 
= values[0as 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.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (type == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (importedTypes[type] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlSchemaComplexType ct 
= (XmlSchemaComplexType)type;
InBlock.gif            
if (ct.Particle is XmlSchemaSequence)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlSchemaObjectCollection items 
= ((XmlSchemaSequence)ct.Particle).Items;
InBlock.gif                
if (items.Count == 2 && items[0is XmlSchemaAny && items[1is XmlSchemaAny)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.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.gifContractedSubBlock.gif                    
dot.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



    真麻烦,我还是喜欢用WriteXML()和ReadXml()方法来处理:)

转载于:https://www.cnblogs.com/happyhippy/archive/2007/05/23/756860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值