By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,761 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,761 IT Pros & Developers. It's quick & easy.

Can not serialize data from webservice

P: n/a
Hi
I anyone have a clue or can solve my problem I would be
glad :-) Regards Michael

I have a problem with creating an XML-document where the
returning data from the webservice, have been serialized.

In my program I am calling a webservice which then return
the data to the calling program. The program then have to
serialize the data and create an XML-document.

When I run the program the following error occur:
Unhandled Exception: System.InvalidOperationException:
There was an error genera
ting the XML document. ---> System.InvalidCastException:
Specified cast is not valid. at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
ionWriter1.Write8_Bil(Object o)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize
(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces
namespaces) at
System.Xml.Serialization.XmlSerializer.Serialize
(TextWriter textWriter, Object o) at
CreateXMLTrainStationInformation.CreateXML.Main() in
C:\Data\DotNET\CreateXMLTrainStationInformation\Cr eateXML.v
b:line 106
If I then cut-and-paste then code from the webservice into
my program, it then runs fine!!

My test-program looks like this:
Imports System
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
.....
.....
.....
Dim WSBIL As New localhost2.GetBil()
Dim bil = New BilType()
bil = WSBIL.GetBil()
Dim Ser As New XmlSerializer(GetType(BilType))
Dim SW As New StreamWriter
("c:\temp\wsTrafficInformation\bil.xml")
Ser.Serialize(SW, bil)
SW.Close()
My webservice looks like this:
<WebMethod()> Public Function GetBil() As BilType
Dim Bil As New BilType()

Bil.Model = "Ford T"
Bil.NrPlade = "ZX 12423"

Bil.Dor = New DorType(1) {}

Bil.Dor(0) = New DorType()
Bil.Dor(0).Farve = "Rød"
Bil.Dor(0).Placering = PlaceringType.Venstre

Bil.Dor(1) = New DorType()
Bil.Dor(1).Farve = "Rød"
Bil.Dor(1).Placering = PlaceringType.Højre

Bil.Saede = New SaedeType(1) {}
Bil.Saede(0) = New SaedeType()
Bil.Saede(0).Placering = PlaceringType.Højre

Bil.Saede(0).Betraek = New BetraekType()
Bil.Saede(0).Betraek.Art = ArtType.Ruskind
Bil.Saede(0).Betraek.Farve = "Grøn"

Bil.Saede = New SaedeType(1) {}
Bil.Saede(1) = New SaedeType()
Bil.Saede(1).Placering = PlaceringType.Venstre

Bil.Saede(1).Betraek = New BetraekType()
Bil.Saede(1).Betraek.Art = ArtType.Læder
Bil.Saede(1).Betraek.Farve = "Rød"
GetBil = Bil
End Function

My classes are generated with the XSD.exe utility and the
code looks like this:
'----------------------------------------------------------
--------------------
' <autogenerated>
' This code was generated by a tool.
' Runtime Version: 1.0.3705.288
'
' Changes to this file may cause incorrect behavior
and will be lost if
' the code is regenerated.
' </autogenerated>
'----------------------------------------------------------
--------------------

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'This source code was auto-generated by xsd,
Version=1.0.3705.288.
'

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\"), _
System.Xml.Serialization.XmlRootAttribute("Bil",
[Namespace]:="c:\data\XML\Bil\", IsNullable:=false)> _
Public Class BilType

'<remarks/>
Public Model As String

'<remarks/>
Public NrPlade As String

'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Dor ")>
_
Public Dor() As DorType

'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Sae de")
_

Public Saede() As SaedeType
End Class

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Class DorType

'<remarks/>
Public Placering As PlaceringType

'<remarks/>
Public Farve As String
End Class

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Enum PlaceringType

'<remarks/>
Venstre

'<remarks/>
Højre
End Enum

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Class BetraekType

'<remarks/>
Public Farve As String

'<remarks/>
Public Art As ArtType
End Class

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Enum ArtType

'<remarks/>
Ruskind

'<remarks/>
Læder

'<remarks/>
Velour

'<remarks/>
Stof
End Enum

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Class SaedeType

'<remarks/>
Public Placering As PlaceringType

'<remarks/>
Public Betraek As BetraekType
End Class

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This client (below) worked for me, almost the same as yours except I
explicitly use the *generated* type (BilType).
This is the definition that gets embedded into the service proxy code
(generated from wsdl.exe).

If you don't want to use that generated type, then I suggest you modify the
generated proxy to cast the return value to an instance of the xsd-generated
type.
Remove the redundant type definitions in that generated file, and when you
compile the client, referenced a DLL that contains definitions for your
shared types (BilType, DorType, etc).

-Dino
Imports System
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization

Public Class TestClient
Public Shared Sub Main()
Try
Dim WSBIL As New localhost2.BilService() ' GetBil
Dim bil = New localhost2.BilType() ''get type definition from the
generated proxy file
bil = WSBIL.GetBil()
Dim Ser As New XmlSerializer(GetType(localhost2.BilType)) '' use the
generated type
Dim SW As New StreamWriter("bil.xml")
Ser.Serialize(SW, bil)
SW.Close()
Catch e as System.Exception
System.Console.WriteLine("Exception: " & e.ToString() )
End Try
End Sub
End Class


"Michael" <we*******@bane.dk> wrote in message
news:0a****************************@phx.gbl...
Hi
I anyone have a clue or can solve my problem I would be
glad :-) Regards Michael

I have a problem with creating an XML-document where the
returning data from the webservice, have been serialized.

In my program I am calling a webservice which then return
the data to the calling program. The program then have to
serialize the data and create an XML-document.

When I run the program the following error occur:
Unhandled Exception: System.InvalidOperationException:
There was an error genera
ting the XML document. ---> System.InvalidCastException:
Specified cast is not valid. at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
ionWriter1.Write8_Bil(Object o)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize
(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces
namespaces) at
System.Xml.Serialization.XmlSerializer.Serialize
(TextWriter textWriter, Object o) at
CreateXMLTrainStationInformation.CreateXML.Main() in
C:\Data\DotNET\CreateXMLTrainStationInformation\Cr eateXML.v
b:line 106
If I then cut-and-paste then code from the webservice into
my program, it then runs fine!!

My test-program looks like this:
Imports System
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
.....
.....
.....
Dim WSBIL As New localhost2.GetBil()
Dim bil = New BilType()
bil = WSBIL.GetBil()
Dim Ser As New XmlSerializer(GetType(BilType))
Dim SW As New StreamWriter
("c:\temp\wsTrafficInformation\bil.xml")
Ser.Serialize(SW, bil)
SW.Close()
My webservice looks like this:
<WebMethod()> Public Function GetBil() As BilType
Dim Bil As New BilType()

Bil.Model = "Ford T"
Bil.NrPlade = "ZX 12423"

Bil.Dor = New DorType(1) {}

Bil.Dor(0) = New DorType()
Bil.Dor(0).Farve = "Rød"
Bil.Dor(0).Placering = PlaceringType.Venstre

Bil.Dor(1) = New DorType()
Bil.Dor(1).Farve = "Rød"
Bil.Dor(1).Placering = PlaceringType.Højre

Bil.Saede = New SaedeType(1) {}
Bil.Saede(0) = New SaedeType()
Bil.Saede(0).Placering = PlaceringType.Højre

Bil.Saede(0).Betraek = New BetraekType()
Bil.Saede(0).Betraek.Art = ArtType.Ruskind
Bil.Saede(0).Betraek.Farve = "Grøn"

Bil.Saede = New SaedeType(1) {}
Bil.Saede(1) = New SaedeType()
Bil.Saede(1).Placering = PlaceringType.Venstre

Bil.Saede(1).Betraek = New BetraekType()
Bil.Saede(1).Betraek.Art = ArtType.Læder
Bil.Saede(1).Betraek.Farve = "Rød"
GetBil = Bil
End Function

My classes are generated with the XSD.exe utility and the
code looks like this:
'----------------------------------------------------------
--------------------
' <autogenerated>
' This code was generated by a tool.
' Runtime Version: 1.0.3705.288
'
' Changes to this file may cause incorrect behavior
and will be lost if
' the code is regenerated.
' </autogenerated>
'----------------------------------------------------------
--------------------

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'This source code was auto-generated by xsd,
Version=1.0.3705.288.
'

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\"), _
System.Xml.Serialization.XmlRootAttribute("Bil",
[Namespace]:="c:\data\XML\Bil\", IsNullable:=false)> _
Public Class BilType

'<remarks/>
Public Model As String

'<remarks/>
Public NrPlade As String

'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Dor ")>
_
Public Dor() As DorType

'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Sae de")
_

Public Saede() As SaedeType
End Class

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Class DorType

'<remarks/>
Public Placering As PlaceringType

'<remarks/>
Public Farve As String
End Class

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Enum PlaceringType

'<remarks/>
Venstre

'<remarks/>
Højre
End Enum

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Class BetraekType

'<remarks/>
Public Farve As String

'<remarks/>
Public Art As ArtType
End Class

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Enum ArtType

'<remarks/>
Ruskind

'<remarks/>
Læder

'<remarks/>
Velour

'<remarks/>
Stof
End Enum

'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute
([Namespace]:="c:\data\XML\Bil\")> _
Public Class SaedeType

'<remarks/>
Public Placering As PlaceringType

'<remarks/>
Public Betraek As BetraekType
End Class
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.