473,581 Members | 3,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlDataDocument over WebService in C#

Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(type of(Items[]))]
public XmlDataDocument GetTypedXmlData Document()
{
sqlDataAdapter1 .Fill(typedData Set1);
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.Servi ce1 proxy = new localhost.Servi ce1();
localhost.Items[] items = proxy.GetTypedX mlDataDocument( );
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.Serv ices.Protocols. SoapException: Server was
unable to process request. --->
System.InvalidO perationExcepti on: There was an error
generating the XML document. --->
System.InvalidC astException: Specified cast is not valid.
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializat
ionWriter1.Writ e16_Test4Respon se(Object[] p) [...]

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

Thanks

Adrian
Nov 21 '05 #1
4 8264
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*******@hotm ail.com> wrote in message
news:O%******** *******@TK2MSFT NGP12.phx.gbl.. .
Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(type of(Items[]))]
public XmlDataDocument GetTypedXmlData Document()
{
sqlDataAdapter1 .Fill(typedData Set1);
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.Servi ce1 proxy = new localhost.Servi ce1();
localhost.Items[] items = proxy.GetTypedX mlDataDocument( );
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.Serv ices.Protocols. SoapException: Server was
unable to process request. --->
System.InvalidO perationExcepti on: There was an error
generating the XML document. --->
System.InvalidC astException: Specified cast is not valid.
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializat
ionWriter1.Writ e16_Test4Respon se(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******** ********@tk2msf tngp13.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*******@hotm ail.com> wrote in message
news:O%******** *******@TK2MSFT NGP12.phx.gbl.. .
Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(type of(Items[]))]
public XmlDataDocument GetTypedXmlData Document()
{
sqlDataAdapter1 .Fill(typedData Set1);
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.Servi ce1 proxy = new localhost.Servi ce1();
localhost.Items[] items = proxy.GetTypedX mlDataDocument( );
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.Serv ices.Protocols. SoapException: Server was
unable to process request. --->
System.InvalidO perationExcepti on: There was an error
generating the XML document. --->
System.InvalidC astException: Specified cast is not valid.
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializat
ionWriter1.Writ e16_Test4Respon se(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*******@hotm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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******** ********@tk2msf tngp13.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*******@hotm ail.com> wrote in message
news:O%******** *******@TK2MSFT NGP12.phx.gbl.. .
Hi,

On the server I code the following web service:

[WebMethod()]
[return: XmlElement(type of(Items[]))]
public XmlDataDocument GetTypedXmlData Document()
{
sqlDataAdapter1 .Fill(typedData Set1);
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.Servi ce1 proxy = new localhost.Servi ce1();
localhost.Items[] items = proxy.GetTypedX mlDataDocument( );
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.Serv ices.Protocols. SoapException: Server was
unable to process request. --->
System.InvalidO perationExcepti on: There was an error
generating the XML document. --->
System.InvalidC astException: Specified cast is not valid.
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializat
ionWriter1.Writ e16_Test4Respon se(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(Descr iption="Mumble mmlmlksalkjkc")]
public System.Xml.XmlD ataDocument GetXmlDataDocum ent()
{
DataSet ds= new DataSet();
ds.DataSetName= "ArrayOfIte m";
ds.Namespace= "Your-XML-Namespace-Here";
System.Data.Sql Client.SqlDataA dapter da=new
System.Data.Sql Client.SqlDataA dapter();
System.Data.Sql Client.SqlConne ction conn= new
System.Data.Sql Client.SqlConne ction(dsn);
da.SelectComman d= new System.Data.Sql Client.SqlComma nd(strSQL, conn);

da.Fill(ds,"Ite m");
System.Xml.XmlD ataDocument dataDoc = new
System.Xml.XmlD ataDocument(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 GetXmlDataDocum entResult . You need to change the
cast for the return value as well.

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

[XmlType(Namespa ce="Your-XML-Namespace-Here")]
[XmlRoot("ArrayO fItem", Namespace="Your-XML-Namespace-Here")]
public class ItemArray {
[XmlElement("Ite m")]
public Item[] a;
}

[XmlType(Namespa ce="Your-XML-Namespace-Here")]
[XmlRoot(Namespa ce="Your-XML-Namespace-Here")]
public class GetXmlDataDocum entResult {
[XmlElement("Arr ayOfItem")]
public ItemArray wrapper;
}

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

GetXmlDataDocum entResult r= service.GetXmlD ataDocument();
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******** *******@TK2MSFT NGP15.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*******@hotm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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******** ********@tk2msf tngp13.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*******@hotm ail.com> wrote in message
news:O%******** *******@TK2MSFT NGP12.phx.gbl.. .
> Hi,
>
> On the server I code the following web service:
>
> [WebMethod()]
> [return: XmlElement(type of(Items[]))]
> public XmlDataDocument GetTypedXmlData Document()
> {
> sqlDataAdapter1 .Fill(typedData Set1);
> 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.Servi ce1 proxy = new localhost.Servi ce1();
> localhost.Items[] items = proxy.GetTypedX mlDataDocument( );
> 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.Serv ices.Protocols. SoapException: Server was
> unable to process request. --->
> System.InvalidO perationExcepti on: There was an error
> generating the XML document. --->
> System.InvalidC astException: Specified cast is not valid.
> at
> Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializat
> ionWriter1.Writ e16_Test4Respon se(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
1302
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 application that has a DataSet with two data tables, Person and Home. I've setup Home as a child of Person. What I want to do is to load data into these...
1
3523
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 asp pages. To do this we're returning a XmlDataDocument ie Public Function GetDataAsXml(...) return new XmlDataDocument(GetDataAsDataSet) End...
0
1314
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 memory? I am new to xml. any suggetions would be highly appretiated.
2
1634
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 classes so i can import/export xml with ease and also i can update my schema without compiling the code. What advantages/disadvantages do you guys...
0
2144
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, and dynamically generate an html table. The problem is when the DataSet contains a null value, the node for that record is not created. Then when...
2
3894
by: James Ankrom | last post by:
Why does this fail? Dim relResources As New Data.DataRelation("Application_Resources", ..Tables("User_Applications").Columns("Application_id"), ..Tables("Resource_Rights").Columns("Application_id"), False) mUserData.Relations.Add(relResources) relResources.Nested = True Dim UserDataXML As System.Xml.XmlDataDocument = New...
1
1249
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 XmlDataDocument(); xml.Load(@"C:\foo.xml"); for(int i = 0; i < 100; i++) { XmlNode node = xml.CloneNode(true);
0
1423
by: Steve | last post by:
I have a dataset. I fill it with two recordsets from SQL queries. Tables are called tblPlanFYSpendingStage, tblSpendingStage.
1
2873
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 Save, I call the subroutine below. The first time that I call it, it works fine. However, if the user makes some more changes in the DataGridView and...
0
7886
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7809
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6569
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5685
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.