472,805 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XmlDataDocument over WebService in C#

Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(typeof(Items[]))]
public XmlDataDocument GetTypedXmlDataDocument()
{
sqlDataAdapter1.Fill(typedDataSet1);
XmlDataDocument dataDoc
= new XmlDataDocument(typedDataSet1);
return dataDoc;
}
Items is a class as follow:

private class Items
{
public int ItemNumber;
public string Description;
public Decimal Price;
}
On the client, after adding a reference to the web service:

localhost.Service1 proxy = new localhost.Service1();
localhost.Items[] items = proxy.GetTypedXmlDataDocument();
string description;
description = items[1].Description;

What the example is attempting to do is to cast the
returned XmlDataDocment xml data stream into an array of
xml elements of type Item.

The web service works when called with 'invoke' from the
automatically generated test web page. Invoke uses the
http protocol. When the web service is called using the
Soap protocol the server returns:

System.Web.Services.Protocols.SoapException: Server was
unable to process request. --->
System.InvalidOperationException: There was an error
generating the XML document. --->
System.InvalidCastException: Specified cast is not valid.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
ionWriter1.Write16_Test4Response(Object[] p) [...]

Can someone point out why this error is occuring and what
the solution is?

Thanks

Adrian
Nov 21 '05 #1
4 8210
the use of a webservice is not germaine to the problem here.

you are casting an object of type X to an object of type Y, and the two
types are not compatible.
in your case X is XmlDataDocument
and Y is Items[]

----

If you want to return something of type Items[], then your webmethod should
be

public Items[] GetStuff() {
...
}

and not...

public XmlDataDocument GetStuff() {
...
}

"Adrian Meyer" <me*******@hotmail.com> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(typeof(Items[]))]
public XmlDataDocument GetTypedXmlDataDocument()
{
sqlDataAdapter1.Fill(typedDataSet1);
XmlDataDocument dataDoc
= new XmlDataDocument(typedDataSet1);
return dataDoc;
}
Items is a class as follow:

private class Items
{
public int ItemNumber;
public string Description;
public Decimal Price;
}
On the client, after adding a reference to the web service:

localhost.Service1 proxy = new localhost.Service1();
localhost.Items[] items = proxy.GetTypedXmlDataDocument();
string description;
description = items[1].Description;

What the example is attempting to do is to cast the
returned XmlDataDocment xml data stream into an array of
xml elements of type Item.

The web service works when called with 'invoke' from the
automatically generated test web page. Invoke uses the
http protocol. When the web service is called using the
Soap protocol the server returns:

System.Web.Services.Protocols.SoapException: Server was
unable to process request. --->
System.InvalidOperationException: There was an error
generating the XML document. --->
System.InvalidCastException: Specified cast is not valid.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
ionWriter1.Write16_Test4Response(Object[] p) [...]

Can someone point out why this error is occuring and what
the solution is?

Thanks

Adrian

Nov 21 '05 #2
Thanks for your replay..

I saw this example on:
http://msdn.microsoft.com/library/de...ce02112003.asp

The third option in the example. This ist used to generate the schema for
the data we are returning and change the WSDL accordingly.

Now, I have to map the XML-Elements manualy into the Items[]-instances? Is
the example from msdn wrong?

Thanks!

Adrian

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> schrieb im
Newsbeitrag news:%2****************@tk2msftngp13.phx.gbl...
the use of a webservice is not germaine to the problem here.

you are casting an object of type X to an object of type Y, and the two
types are not compatible.
in your case X is XmlDataDocument
and Y is Items[]

----

If you want to return something of type Items[], then your webmethod should be

public Items[] GetStuff() {
...
}

and not...

public XmlDataDocument GetStuff() {
...
}

"Adrian Meyer" <me*******@hotmail.com> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(typeof(Items[]))]
public XmlDataDocument GetTypedXmlDataDocument()
{
sqlDataAdapter1.Fill(typedDataSet1);
XmlDataDocument dataDoc
= new XmlDataDocument(typedDataSet1);
return dataDoc;
}
Items is a class as follow:

private class Items
{
public int ItemNumber;
public string Description;
public Decimal Price;
}
On the client, after adding a reference to the web service:

localhost.Service1 proxy = new localhost.Service1();
localhost.Items[] items = proxy.GetTypedXmlDataDocument();
string description;
description = items[1].Description;

What the example is attempting to do is to cast the
returned XmlDataDocment xml data stream into an array of
xml elements of type Item.

The web service works when called with 'invoke' from the
automatically generated test web page. Invoke uses the
http protocol. When the web service is called using the
Soap protocol the server returns:

System.Web.Services.Protocols.SoapException: Server was
unable to process request. --->
System.InvalidOperationException: There was an error
generating the XML document. --->
System.InvalidCastException: Specified cast is not valid.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
ionWriter1.Write16_Test4Response(Object[] p) [...]

Can someone point out why this error is occuring and what
the solution is?

Thanks

Adrian


Nov 21 '05 #3
ok, my bad.
sorry, I failed to see the XmlElement attribute.

Is the MSDN article wrong?
It seems so? There are other people who have questioned it, none have got
an answer that shows how to successfully do it.
One poster got a response that said, "yep, this is a bug".

http://groups.google.com/groups?num=...sp&btnG=Search

-Dino
"Adrian Meyer" <me*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Thanks for your replay..

I saw this example on:
http://msdn.microsoft.com/library/de...ce02112003.asp
The third option in the example. This ist used to generate the schema for
the data we are returning and change the WSDL accordingly.

Now, I have to map the XML-Elements manualy into the Items[]-instances? Is
the example from msdn wrong?

Thanks!

Adrian

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> schrieb im
Newsbeitrag news:%2****************@tk2msftngp13.phx.gbl...
the use of a webservice is not germaine to the problem here.

you are casting an object of type X to an object of type Y, and the two
types are not compatible.
in your case X is XmlDataDocument
and Y is Items[]

----

If you want to return something of type Items[], then your webmethod

should
be

public Items[] GetStuff() {
...
}

and not...

public XmlDataDocument GetStuff() {
...
}

"Adrian Meyer" <me*******@hotmail.com> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(typeof(Items[]))]
public XmlDataDocument GetTypedXmlDataDocument()
{
sqlDataAdapter1.Fill(typedDataSet1);
XmlDataDocument dataDoc
= new XmlDataDocument(typedDataSet1);
return dataDoc;
}
Items is a class as follow:

private class Items
{
public int ItemNumber;
public string Description;
public Decimal Price;
}
On the client, after adding a reference to the web service:

localhost.Service1 proxy = new localhost.Service1();
localhost.Items[] items = proxy.GetTypedXmlDataDocument();
string description;
description = items[1].Description;

What the example is attempting to do is to cast the
returned XmlDataDocment xml data stream into an array of
xml elements of type Item.

The web service works when called with 'invoke' from the
automatically generated test web page. Invoke uses the
http protocol. When the web service is called using the
Soap protocol the server returns:

System.Web.Services.Protocols.SoapException: Server was
unable to process request. --->
System.InvalidOperationException: There was an error
generating the XML document. --->
System.InvalidCastException: Specified cast is not valid.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
ionWriter1.Write16_Test4Response(Object[] p) [...]

Can someone point out why this error is occuring and what
the solution is?

Thanks

Adrian



Nov 21 '05 #4
one more thing.
It is possible to hack it so it works. Not pretty, but possible.

The Invalid Cast error happens on the server side in serialization, before
your client ever gets the chance to get the XML. If you eliminate the
[return: XmlElement()] on the webmethod, you will avoid that problem.

So here's what I did.

1. Write the server-side method to return XmlDataDocument. You don't need
the [return: XmlElement(..)] attribute. It will look like this:
[WebMethod(Description="Mumble mmlmlksalkjkc")]
public System.Xml.XmlDataDocument GetXmlDataDocument()
{
DataSet ds= new DataSet();
ds.DataSetName="ArrayOfItem";
ds.Namespace= "Your-XML-Namespace-Here";
System.Data.SqlClient.SqlDataAdapter da=new
System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlConnection conn= new
System.Data.SqlClient.SqlConnection(dsn);
da.SelectCommand= new System.Data.SqlClient.SqlCommand(strSQL, conn);

da.Fill(ds,"Item");
System.Xml.XmlDataDocument dataDoc = new
System.Xml.XmlDataDocument(ds);
return dataDoc;
}

(You don't need a typed dataset, it works with untyped datasets as well)

2. Generate the client-side proxy using wsdl.exe. The proxy method will
have a return type of XmlNode.

3. Modify the generated client-side proxy: change the return value of the
method from XmlNode to GetXmlDataDocumentResult . You need to change the
cast for the return value as well.

4. Modify the generated client-side proxy: include these new types:

[XmlType(Namespace="Your-XML-Namespace-Here")]
[XmlRoot("ArrayOfItem", Namespace="Your-XML-Namespace-Here")]
public class ItemArray {
[XmlElement("Item")]
public Item[] a;
}

[XmlType(Namespace="Your-XML-Namespace-Here")]
[XmlRoot(Namespace="Your-XML-Namespace-Here")]
public class GetXmlDataDocumentResult {
[XmlElement("ArrayOfItem")]
public ItemArray wrapper;
}

5. To get at the array of Items in a client app, you need to do something
like this:

GetXmlDataDocumentResult r= service.GetXmlDataDocument();
Item[] a= r.wrapper.a;
6. compile and run.

note:
Look out for the Xml namespaces in the above code- they should agree with
whatever you specified for your web service.
If you are using typed datasets then you do not need to set the DataSetName
or the namespace as they may already be set.
"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
ok, my bad.
sorry, I failed to see the XmlElement attribute.

Is the MSDN article wrong?
It seems so? There are other people who have questioned it, none have got
an answer that shows how to successfully do it.
One poster got a response that said, "yep, this is a bug".

http://groups.google.com/groups?num=...sp&btnG=Search
-Dino
"Adrian Meyer" <me*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Thanks for your replay..

I saw this example on:

http://msdn.microsoft.com/library/de...ce02112003.asp

The third option in the example. This ist used to generate the schema for the data we are returning and change the WSDL accordingly.

Now, I have to map the XML-Elements manualy into the Items[]-instances? Is the example from msdn wrong?

Thanks!

Adrian

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> schrieb im
Newsbeitrag news:%2****************@tk2msftngp13.phx.gbl...
the use of a webservice is not germaine to the problem here.

you are casting an object of type X to an object of type Y, and the two types are not compatible.
in your case X is XmlDataDocument
and Y is Items[]

----

If you want to return something of type Items[], then your webmethod

should
be

public Items[] GetStuff() {
...
}

and not...

public XmlDataDocument GetStuff() {
...
}

"Adrian Meyer" <me*******@hotmail.com> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> On the server I code the following web service:
>
> [WebMethod()]
> [return: XmlElement(typeof(Items[]))]
> public XmlDataDocument GetTypedXmlDataDocument()
> {
> sqlDataAdapter1.Fill(typedDataSet1);
> XmlDataDocument dataDoc
> = new XmlDataDocument(typedDataSet1);
> return dataDoc;
> }
>
>
> Items is a class as follow:
>
> private class Items
> {
> public int ItemNumber;
> public string Description;
> public Decimal Price;
> }
>
>
> On the client, after adding a reference to the web service:
>
> localhost.Service1 proxy = new localhost.Service1();
> localhost.Items[] items = proxy.GetTypedXmlDataDocument();
> string description;
> description = items[1].Description;
>
> What the example is attempting to do is to cast the
> returned XmlDataDocment xml data stream into an array of
> xml elements of type Item.
>
> The web service works when called with 'invoke' from the
> automatically generated test web page. Invoke uses the
> http protocol. When the web service is called using the
> Soap protocol the server returns:
>
> System.Web.Services.Protocols.SoapException: Server was
> unable to process request. --->
> System.InvalidOperationException: There was an error
> generating the XML document. --->
> System.InvalidCastException: Specified cast is not valid.
> at
> Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializat
> ionWriter1.Write16_Test4Response(Object[] p) [...]
>
> Can someone point out why this error is occuring and what
> the solution is?
>
> Thanks
>
> Adrian
>
>



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mathew Relick | last post by:
I'm relatively new to this sort of thing, so forgive me if this is completely off base, but I'm having a problem getting the XMLDataDocument to do what I need it to. I have a small test...
1
by: Peter Feakins | last post by:
We're building some web services that are primarily being consumed by a .net application. We're returning datasets for that purpose. We're also have to provide access to these services to classic...
0
by: Raj | last post by:
hi, I associate dataset with one xmldatadocument? how can I disassociate from xmldatadocument, so I can associate with another xmldatadocument. and, how can I remove xmldatadocument from...
2
by: Vish | last post by:
Hi, I have a set of nested object classes that now i have to serialize/deserialize to make webservice calls. I was thinking wouldn't it be better to use an XML Data document instead of using...
0
by: gilly3 | last post by:
Reposting to a more relevant group. I am passing a generic dataset into a new XMLDataDocument and then parsing the XML with XSLT. The idea being that I can parse any dataset with the same xslt,...
2
by: James Ankrom | last post by:
Why does this fail? Dim relResources As New Data.DataRelation("Application_Resources", ..Tables("User_Applications").Columns("Application_id"),...
1
by: Petr Felzmann | last post by:
Hi, why the duration of call the CloneNode(true) is steadily increasing? First call is 0.004s and 100th is 0.16s! XML file is 5kB only, no disk swap. XmlDataDocument xml = new...
0
by: Steve | last post by:
I have a dataset. I fill it with two recordsets from SQL queries. Tables are called tblPlanFYSpendingStage, tblSpendingStage.
1
by: JD | last post by:
I have a DataGridView with a DataSet as DataSource. The user can update the contents of the DataGridView, and then click on a Save button to save the data to an XML file. When they click on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.