473,387 Members | 1,520 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,387 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 8251
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.