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