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