Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > XmlSerializer and the XmlRootAttribute ?

Reply
Thread Tools

XmlSerializer and the XmlRootAttribute ?

 
 
Pmcg
Guest
Posts: n/a
 
      11-09-2004
Hi,
I'm having trouble controlling the Xml content that is created by the
XmlSerializer when i make a web service call from a proxy, i thought the
XmlRootAttribute would take care of this
but it seems to have no bearing on the Xml created by the XmlSerializer when
i attempt to make the web service call. If anyone has figured out how to
control the element name for a type in the
generated Xml i would appreciate a pointer.

Below is a simple version of what i have done, where i have customised the
proxy generated by the wsdl.exe tool, i can get this to work by using the
typeName property for the XmlElementAttribute, but i would prefer to apply
this directly to the class as it is used for multiple methods

Server side
If i have a classes such as
[XmlRootAttribute("A")]
public class ServerLongClassName1
{
public int f1;
}
[XmlRootAttribute("R")]
public class ServerLongClassName2
{
public int f1;
}

and i have a web method like
[WebMethodAttribute(Description=".....")]
public LongClassName2 MyMethod(LongClassName1 arg)
{
.....
}



Client side
[XmlRootAttribute("A")]
public class ClientLongClassName1
{
public int f1;
}
[XmlRootAttribute("R")]
public class ClientLongClassName2
{
public int f1;
}

and i have a web proxy class that looks like
[DebuggerStepThroughAttribute()]
[DesignerCategoryAttribute("code")]
[WebServiceBindingAttribute(Name="XSoap", Namespace="http://Something")]
public sealed class olctg : SoapHttpClientProtocol
{
[SoapDocumentMethodAttribute("http://Something/MyMethod",
RequestNamespace="http://Something",
ResponseNamespace="http://Something",
Use=System.Web.Services.Description.SoapBindingUse .Literal,
ParameterStyle=System.Web.Services.Protocols.SoapP arameterStyle.Wrapped)]
[return: XmlElementAttribute("R", IsNullable=true)]
public ClientLongClassName2 MyMethod(ClientLongClassName1 p)
{
// Check parameter
if (req == null)
throw new ArgumentNullException("req");

// Make call
object[] results = this.Invoke("DoLookup", new object[] {req});
return ((GCX.ServiceProxy.OLCTG.bmibaby20040426.Response) (results[0]));
}
}



Thanks in advance
Pat
 
Reply With Quote
 
 
 
 
Pmcg
Guest
Posts: n/a
 
      11-09-2004
Apologies,
I left some details in the proxy class MyMethod method by mistake, you can
ignore this as this was a simplistic version of what i was doing, code
snippet was just an illustration.

Thanks
Pat

"Pmcg" wrote:

> Hi,
> I'm having trouble controlling the Xml content that is created by the
> XmlSerializer when i make a web service call from a proxy, i thought the
> XmlRootAttribute would take care of this
> but it seems to have no bearing on the Xml created by the XmlSerializer when
> i attempt to make the web service call. If anyone has figured out how to
> control the element name for a type in the
> generated Xml i would appreciate a pointer.
>
> Below is a simple version of what i have done, where i have customised the
> proxy generated by the wsdl.exe tool, i can get this to work by using the
> typeName property for the XmlElementAttribute, but i would prefer to apply
> this directly to the class as it is used for multiple methods
>
> Server side
> If i have a classes such as
> [XmlRootAttribute("A")]
> public class ServerLongClassName1
> {
> public int f1;
> }
> [XmlRootAttribute("R")]
> public class ServerLongClassName2
> {
> public int f1;
> }
>
> and i have a web method like
> [WebMethodAttribute(Description=".....")]
> public LongClassName2 MyMethod(LongClassName1 arg)
> {
> .....
> }
>
>
>
> Client side
> [XmlRootAttribute("A")]
> public class ClientLongClassName1
> {
> public int f1;
> }
> [XmlRootAttribute("R")]
> public class ClientLongClassName2
> {
> public int f1;
> }
>
> and i have a web proxy class that looks like
> [DebuggerStepThroughAttribute()]
> [DesignerCategoryAttribute("code")]
> [WebServiceBindingAttribute(Name="XSoap", Namespace="http://Something")]
> public sealed class olctg : SoapHttpClientProtocol
> {
> [SoapDocumentMethodAttribute("http://Something/MyMethod",
> RequestNamespace="http://Something",
> ResponseNamespace="http://Something",
> Use=System.Web.Services.Description.SoapBindingUse .Literal,
> ParameterStyle=System.Web.Services.Protocols.SoapP arameterStyle.Wrapped)]
> [return: XmlElementAttribute("R", IsNullable=true)]
> public ClientLongClassName2 MyMethod(ClientLongClassName1 p)
> {
> // Check parameter
> if (req == null)
> throw new ArgumentNullException("req");
>
> // Make call
> object[] results = this.Invoke("DoLookup", new object[] {req});
> return ((GCX.ServiceProxy.OLCTG.bmibaby20040426.Response) (results[0]));
> }
> }
>
>
>
> Thanks in advance
> Pat

 
Reply With Quote
 
 
 
 
Pmcg
Guest
Posts: n/a
 
      11-09-2004
Thanks for the reply Christoph, it is very much appreciated.
Am i correct in saying that you really need to annotate each return type and
parameter type with an XmlElementAttribute to control the XML element name
where you do not want to use the defaults ?
I presume this applies equally so for the proxy class methods, which should
have the exact same typeName property values on the XmlElementAttributes on
the proxy web methods.

Is it posibble in the case of an array parameter to indicate the enclosing
element name and item element names, such as in the case of
public bool MyMethod(MyArg[] p1) { ... }
where we could get p1 serialised as something like
<param1><param1Item>....</param1Item><param1Item>....</param1Item></param1>


Thanks
Pat
 
Reply With Quote
 
Dan Rogers
Guest
Posts: n/a
 
      11-11-2004
Hi Pat,

You can gain very tight control over the names and namespaces that are part
of the serialized XML. The attributes you want to focus on for your
problem are either the XmlRootAttribute, XmlElementAttribute,
XmlTypeAttribute and XmlAttributeAttribute. These all let you specify name
information.

If you have a schema for your XML, you can see really detailed example of
class mark-up by generating the code for your classes from your schemas.
Many people who are building cross platform web services start by defining
the schema for their types (parameters and return types), and then
generating the appropriate class logic instead of hand coding it. The
Xsd.exe and XsdObjectGen.exe tools both will show you how a given
class/property should be marked up to achieve a given XML result described
by schema metadata.

Sometimes a generated example like this is the best way to learn the subtle
nuances.

Best regards

Dan Rogers (danro)
Microsoft Corporation
--------------------
>Thread-Topic: XmlSerializer and the XmlRootAttribute ?
>thread-index: AcTGlTGr6Ph7B9PQTQeGS19TIUJIbQ==
>X-WBNR-Posting-Host: 193.120.138.174
>From: "=?Utf-8?B?UG1jZw==?=" <(E-Mail Removed)>
>Subject: XmlSerializer and the XmlRootAttribute ?
>Date: Tue, 9 Nov 2004 11:49:15 -0800
>Lines: 76
>Message-ID: <(E-Mail Removed)>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="Utf-8"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Content-Class: urn:content-classes:message
>Importance: normal
>Priority: normal
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
>Newsgroups: microsoft.public.dotnet.framework.aspnet.webservic es
>NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
>Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
>Xref: cpmsftngxa10.phx.gbl

microsoft.public.dotnet.framework.aspnet.webservic es:26423
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservic es
>
>Hi,
>I'm having trouble controlling the Xml content that is created by the
>XmlSerializer when i make a web service call from a proxy, i thought the
>XmlRootAttribute would take care of this
>but it seems to have no bearing on the Xml created by the XmlSerializer

when
>i attempt to make the web service call. If anyone has figured out how to
>control the element name for a type in the
>generated Xml i would appreciate a pointer.
>
>Below is a simple version of what i have done, where i have customised the
>proxy generated by the wsdl.exe tool, i can get this to work by using the
>typeName property for the XmlElementAttribute, but i would prefer to apply
>this directly to the class as it is used for multiple methods
>
>Server side
>If i have a classes such as
>[XmlRootAttribute("A")]
>public class ServerLongClassName1
>{
> public int f1;
>}
>[XmlRootAttribute("R")]
>public class ServerLongClassName2
>{
> public int f1;
>}
>
>and i have a web method like
>[WebMethodAttribute(Description=".....")]
>public LongClassName2 MyMethod(LongClassName1 arg)
>{
> .....
>}
>
>
>
>Client side
>[XmlRootAttribute("A")]
>public class ClientLongClassName1
>{
> public int f1;
>}
>[XmlRootAttribute("R")]
>public class ClientLongClassName2
>{
> public int f1;
>}
>
>and i have a web proxy class that looks like
>[DebuggerStepThroughAttribute()]
>[DesignerCategoryAttribute("code")]
>[WebServiceBindingAttribute(Name="XSoap", Namespace="http://Something")]
>public sealed class olctg : SoapHttpClientProtocol
>{
> [SoapDocumentMethodAttribute("http://Something/MyMethod",
> RequestNamespace="http://Something",
> ResponseNamespace="http://Something",
> Use=System.Web.Services.Description.SoapBindingUse .Literal,
> ParameterStyle=System.Web.Services.Protocols.SoapP arameterStyle.Wrapped)]
> [return: XmlElementAttribute("R", IsNullable=true)]
> public ClientLongClassName2 MyMethod(ClientLongClassName1 p)
> {
> // Check parameter
> if (req == null)
> throw new ArgumentNullException("req");
>
> // Make call
> object[] results = this.Invoke("DoLookup", new object[] {req});
> return ((GCX.ServiceProxy.OLCTG.bmibaby20040426.Response) (results[0]));
> }
>}
>
>
>
>Thanks in advance
>Pat
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OuputFormat and XMLSerializer's alternative? Denny Java 1 06-06-2008 02:11 AM
Desperate please help with XmlSerializer, IXmlSerializable and XmlSchemaProvider peter.moss@gmail.com ASP .Net Web Services 0 04-10-2006 04:34 PM
XMLSerializer and Parametersless Constructors Michael Brockman ASP .Net 2 10-17-2004 04:36 PM
XmlSerializer - Cannot execute a program csc.exe =?Utf-8?B?QW5kcmV3RG90SGF5?= ASP .Net 4 08-04-2004 10:39 AM
XmlSerializer constructor error: can't find ...dll zimmy ASP .Net 0 07-07-2004 07:00 PM



Advertisments