473,418 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Can not serialize data from webservice

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
1 2923
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: IanT | last post by:
Hi I need help with my approach to validating de-serialized XML received via webservice. Hopefully someone can point me to sources detailing best practice for the following. I agreed a...
1
by: Roberto Carriquiry | last post by:
I am writeing a webService that needs to recieve a CDO.Person object as a parameter. but when I run it I have a runtime error. Cannot serialize member CDO.PersonClass.DataSource of type...
1
by: Raed Sawalha | last post by:
we have already built Web Application,we did changes on in class in application so , we need to pass a Hashtable object to a webserivce, we're using XML Serialization , my problem in how to...
0
by: Clas | last post by:
Hi! I have some questions about exceptionshandling when using webservices. I want to be able to get the original exception back from the webservice-soapexception. I have used the SoapFormatter to...
0
by: ZWeng | last post by:
I have a webservice taking an object and serrializing it to an xml file. This webservice sits inside of corporate network. Is there a way to serialize the xml to a computer in DMZ? I am using...
6
by: tshad | last post by:
I had asked about this before, but have been unable to solve the problem. Could this be a problem with Web Services? I have 4 web services that I reference in this object. There is only one...
6
by: Paez | last post by:
Hello there. My teacher asked me to do a job and I don't know how.. This is the scenario: I must create a client/server application. The server application is a c# web server and the client...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
2
by: Steph | last post by:
hello, i have a classe. I want serialize it into a define encoding string ... and send it by webservice. XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.Encoding...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.