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
>
>