且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Web Service返回DataTable(zz)

更新时间:2022-09-16 10:30:39


找了好久才找到的:)
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:

Web Service返回DataTable(zz)<s:complexType>
Web Service返回DataTable(zz)    
<s:sequence>
Web Service返回DataTable(zz)      
<s:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/2001/XMLSchema" processContents="lax" />
Web Service返回DataTable(zz)      
<s:any minOccurs="1" namespace="urn:schemas-microsoft-com:xml-diffgram-v1" processContents="lax" />
Web Service返回DataTable(zz)    
</s:sequence>
Web Service返回DataTable(zz)
</s:complexType>
Web Service返回DataTable(zz)

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

Web Service返回DataTable(zz)class DataTableSchemaImporterExtension : SchemaImporterExtension
Web Service返回DataTable(zz)
{
Web Service返回DataTable(zz)    
// DataTableSchemaImporterExtension is used for WebServices, it is used to recognize the schema for DataTable within wsdl
Web Service返回DataTable(zz)
    Hashtable importedTypes = new Hashtable();
Web Service返回DataTable(zz)
Web Service返回DataTable(zz)    
public override string ImportSchemaType(string name, string schemaNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
Web Service返回DataTable(zz)    
{
Web Service返回DataTable(zz)        IList values 
= schemas.GetSchemas(schemaNamespace);
Web Service返回DataTable(zz)        
if (values.Count != 1)
Web Service返回DataTable(zz)        
{
Web Service返回DataTable(zz)            
return null;
Web Service返回DataTable(zz)        }

Web Service返回DataTable(zz)        XmlSchema schema 
= values[0as XmlSchema;
Web Service返回DataTable(zz)        
if (schema == null)
Web Service返回DataTable(zz)            
return null;
Web Service返回DataTable(zz)        XmlSchemaType type 
= (XmlSchemaType)schema.SchemaTypes[new XmlQualifiedName(name, schemaNamespace)];
Web Service返回DataTable(zz)        
return ImportSchemaType(type, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
Web Service返回DataTable(zz)    }

Web Service返回DataTable(zz)
Web Service返回DataTable(zz)    
public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
Web Service返回DataTable(zz)    
{
Web Service返回DataTable(zz)        
if (type == null)
Web Service返回DataTable(zz)        
{
Web Service返回DataTable(zz)            
return null;
Web Service返回DataTable(zz)        }

Web Service返回DataTable(zz)        
if (importedTypes[type] != null)
Web Service返回DataTable(zz)        
{
Web Service返回DataTable(zz)            mainNamespace.Imports.Add(
new CodeNamespaceImport(typeof(DataSet).Namespace));
Web Service返回DataTable(zz)           compileUnit.ReferencedAssemblies.Add(
"System.Data.dll");
Web Service返回DataTable(zz)            
return (string)importedTypes[type];
Web Service返回DataTable(zz)        }

Web Service返回DataTable(zz)        
if (!(context is XmlSchemaElement))
Web Service返回DataTable(zz)            
return null;
Web Service返回DataTable(zz)        
if (type is XmlSchemaComplexType)
Web Service返回DataTable(zz)        
{
Web Service返回DataTable(zz)            XmlSchemaComplexType ct 
= (XmlSchemaComplexType)type;
Web Service返回DataTable(zz)            
if (ct.Particle is XmlSchemaSequence)
Web Service返回DataTable(zz)            
{
Web Service返回DataTable(zz)                XmlSchemaObjectCollection items 
= ((XmlSchemaSequence)ct.Particle).Items;
Web Service返回DataTable(zz)                
if (items.Count == 2 && items[0is XmlSchemaAny && items[1is XmlSchemaAny)
Web Service返回DataTable(zz)                
{
Web Service返回DataTable(zz)                    XmlSchemaAny any0 
= (XmlSchemaAny)items[0];
Web Service返回DataTable(zz)                    XmlSchemaAny any1 
= (XmlSchemaAny)items[1];
Web Service返回DataTable(zz)                    
if (any0.Namespace == XmlSchema.Namespace && any1.Namespace == "urn:schemas-microsoft-com:xml-diffgram-v1")
Web Service返回DataTable(zz)                    
{
Web Service返回DataTable(zz)                        
string typeName = typeof(DataTable).FullName;
Web Service返回DataTable(zz)                        importedTypes.Add(type, typeName);
Web Service返回DataTable(zz)                      mainNamespace.Imports.Add(
new CodeNamespaceImport(typeof(DataTable).Namespace));
Web Service返回DataTable(zz)           compileUnit.ReferencedAssemblies.Add(
"System.Data.dll");
Web Service返回DataTable(zz)                        
return typeName;
Web Service返回DataTable(zz)                    }

Web Service返回DataTable(zz)                }

Web Service返回DataTable(zz)            }

Web Service返回DataTable(zz)        }

Web Service返回DataTable(zz)        
return null;
Web Service返回DataTable(zz)    }

Web Service返回DataTable(zz)}

 

  1. Compile and GAC the SchemaImporterExtension
  2. Add it to the existent extensions in machine.config, ysing fully-qualified assembly name
    Web Service返回DataTable(zz)<system.xml.serialization>
    Web Service返回DataTable(zz)   
    <schemaImporterExtensions>
    Web Service返回DataTable(zz)        
    <add name="DataTableSchemaImporterExtension" type="DataTableSchemaImporterExtension,Web Service返回DataTable(zz)        </schemaImporterExtensions>
    Web Service返回DataTable(zz)
    </system.xml.serialization>
    Web Service返回DataTable(zz)



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