XmlSerializer and the XmlRootAttribute ?

Discussion in 'ASP .Net Web Services' started by Pmcg, Nov 9, 2004.

  1. Pmcg

    Pmcg Guest

    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
    Pmcg, Nov 9, 2004
    #1
    1. Advertising

  2. Pmcg

    Pmcg Guest

    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.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
    Pmcg, Nov 9, 2004
    #2
    1. Advertising

  3. Pmcg

    Pmcg Guest

    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
    Pmcg, Nov 9, 2004
    #3
  4. Pmcg

    Dan Rogers Guest

    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==?=" <>
    >Subject: XmlSerializer and the XmlRootAttribute ?
    >Date: Tue, 9 Nov 2004 11:49:15 -0800
    >Lines: 76
    >Message-ID: <>
    >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
    >
    Dan Rogers, Nov 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5kcmV3RG90SGF5?=

    XmlSerializer - Cannot execute a program csc.exe

    =?Utf-8?B?QW5kcmV3RG90SGF5?=, Jun 13, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,248
    =?Utf-8?B?U2ltb25lIENoaWFyZXR0YQ==?=
    Aug 4, 2004
  2. zimmy
    Replies:
    0
    Views:
    405
    zimmy
    Jul 7, 2004
  3. Michael Brockman

    XMLSerializer and Parametersless Constructors

    Michael Brockman, Oct 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    490
    Michael Brockman
    Oct 17, 2004
  4. Denny
    Replies:
    1
    Views:
    761
    Arne Vajhøj
    Jun 6, 2008
  5. Replies:
    0
    Views:
    305
Loading...

Share This Page