Re: Returning DataTables from Web Services in ASP.NET 2.0

Tech-Archive recommends: Fix windows errors by optimizing your registry



I managed to get this working using asolution from
http://www.microsoft.com/belux/msdn/nl/community/columns/jdruyts/wsproxy.mspx

heres a quote from the page that is useful
"First of all, if you don't want to modify the machine.config file, you
can put the configuration section mentioned above in the application's
configuration. For Visual Studio 2005, this file is typically located
at C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\devenv.exe.config. ... Second, if you would like to
refrain from adding assemblies to the GAC, you can put the assembly in
the same directory as the application. Visual Studio even has a
location specifically created for such design-time helper assemblies:
its PrivateAssemblies directory, which is located right below the
directory in which the devenv.exe.config file can be found."

So for those that are looking for the complete solution here it is.

1. Create a Class Library Project called "DataTableSchemaImporter"
(name can be anything you like)

2. Add a Class called "DataTableSchemaImporterExtension" (again name it
what you like)

3. Add the following code the class (converted code from link in my
first post)
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.CodeDom
Imports System.CodeDom.Compiler

Class DataTableSchemaImporterExtension
Inherits Advanced.SchemaImporterExtension

'-- DataTableSchemaImporterExtension is used for WebServices,
'-- it is used to recognize the schema for DataTable within wsdl
Dim importedTypes As Hashtable = New Hashtable()



Public Overrides Function ImportSchemaType(ByVal name As String,
ByVal schemaNamespace As String, _
ByVal context As XmlSchemaObject, ByVal schemas As XmlSchemas,
ByVal importer As XmlSchemaImporter, _
ByVal compileUnit As CodeCompileUnit, ByVal mainNamespace As
CodeNamespace, ByVal options As CodeGenerationOptions, _
ByVal codeProvider As CodeDomProvider) As String
Dim values As IList = schemas.GetSchemas(schemaNamespace)

If Not values.Count = 1 Then
Return Nothing
End If

Dim schema As XmlSchema = CType(values(0), XmlSchema)

If IsNothing(schema) Then
Return Nothing
End If

Dim type As XmlSchemaType = CType(schema.SchemaTypes(New
XmlQualifiedName(name, schemaNamespace)), XmlSchemaType)

Return ImportSchemaType(type, context, schemas, importer,
compileUnit, mainNamespace, options, codeProvider)
End Function


Public Overrides Function ImportSchemaType(ByVal type As
XmlSchemaType, ByVal context As XmlSchemaObject, _
ByVal schemas As XmlSchemas, ByVal importer As XmlSchemaImporter,
ByVal compileUnit As CodeCompileUnit, _
ByVal mainNamespace As CodeNamespace, ByVal options As
CodeGenerationOptions, _
ByVal codeProvider As CodeDomProvider) As String


If IsNothing(type) Then
Return (Nothing)
End If

If importedTypes(type) IsNot Nothing Then
mainNamespace.Imports.Add(New
CodeNamespaceImport(GetType(DataSet).Namespace))
compileUnit.ReferencedAssemblies.Add("System.Data.dll")
Return CType(importedTypes(type), String)
End If

If Not TypeOf context Is XmlSchemaElement Then
Return Nothing
End If

If TypeOf type Is XmlSchemaComplexType Then
Dim ct As XmlSchemaComplexType = CType(type,
XmlSchemaComplexType)

If TypeOf ct.Particle Is XmlSchemaSequence Then
Dim items As XmlSchemaObjectCollection =
(CType(ct.Particle, XmlSchemaSequence)).Items

If items.Count = 2 AndAlso TypeOf items(0) Is
XmlSchemaAny AndAlso TypeOf items(1) Is XmlSchemaAny Then
Dim any0 As XmlSchemaAny = CType(items(0),
XmlSchemaAny)
Dim any1 As XmlSchemaAny = CType(items(1),
XmlSchemaAny)

If any0.Namespace = XmlSchema.Namespace _
AndAlso any1.Namespace =
"urn:schemas-microsoft-com:xml-diffgram-v1" Then
Dim typeName As String =
GetType(DataTable).FullName
importedTypes.Add(type, typeName)
mainNamespace.Imports.Add(New
CodeNamespaceImport(GetType(DataTable).Namespace))

compileUnit.ReferencedAssemblies.Add("System.Data.dll")
Return typeName
End If
End If
End If
End If

Return Nothing
End Function

End Class

4. Build Project

5. Copy project dll to "<VS 2005 installation
folder>\Common7\IDE\PrivateAssemblies"
where <VS 2005 installation folder> is usally C:\Program
Files\Microsoft Visual Studio 8

6. Open for editing devenv.exe.config in <VS 2005 installation
folder>\Common7\IDE

7. Above </configuration> add

<system.xml.serialization>
<schemaImporterExtensions>
<add name="DataTableSchemaImporterExtension"

type="DataTableSchemaImporter.DataTableSchemaImporterExtension,
DataTableSchemaImporter" />
</schemaImporterExtensions>
</system.xml.serialization>

8. Save file, open project and update web references

.