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        
    None.gif
    system.xml.serialization>
    None.gif



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

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

你可能感兴趣的:(Web Service返回DataTable(zz))