XmlSerializer and the XmlRootAttribute ?

P

Pmcg

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.SoapParameterStyle.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
 
P

Pmcg

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
 
P

Pmcg

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
 
D

Dan Rogers

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==?=" <[email protected]>
Subject: XmlSerializer and the XmlRootAttribute ?
Date: Tue, 9 Nov 2004 11:49:15 -0800
Lines: 76
Message-ID: <[email protected]>
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.webservices
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.webservices:26423
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top