locked
AnonymousType Usage on XSD.exe Generated Code RRS feed

  • Question


  • I am using xsd.exe to generate C# classes from my XSD file. When I look at the generated code I always find the following attribute before every class generated:

     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=http://cns.cloudapp.net/xsd)]

    The inclusion of this flag causes extra classes to be generated in the .cs file which in turn cause name collision in my project. If I simply comment out this attribute everything works fine.

    Is there a way to turn off the generation of this attribute by xsd.exe? I seem to have a manual workaround but I would prefer a clean toolflow without hand-modification of auto-generated code.

    ercercerc
    Monday, April 27, 2009 12:05 AM

Answers

  • I see now. What I didn't get is that you had to turn it off on the server to prevent conflicts in the client.

    There is no way to change this, as far as I know. You'll have to change the schema to not use an anonymous type:

    <xs:element name="CNSClient" type="tns:CNSClientType"/>
    <xs:complexType name="CNSClientType">
      <xs:attribute name="GUID" type="xs:string" use="required" />
      <xs:attribute name="IsActive" type="xs:boolean" use="required" />
    </xs:complexType>
    Without this change, no matter what you do, XSD.EXE will need to invent a type name.
    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    • Marked as answer by ercercerc Wednesday, April 29, 2009 7:26 PM
    Wednesday, April 29, 2009 6:55 PM
    Moderator

All replies

  • Please give an example of the XSD that generates this, and the class it generates.
    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Monday, April 27, 2009 10:11 PM
    Moderator

  • Thanks. Here are four code extracts. If you look at code 3) you will see my WebMethod specifies a parameter type CNSClient. If you look at code 4) you will see Silverlight sees the same function with a parameter type Client (not CNSClient). This type is not in my XSD or in teh code generated by xsd.exe. I assume the type Client was generated by the generation of the web service or the import in the Silverlight.

    If I comment out the lien with - [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true - the type CNSClient is used and the ytpe Client is never generated. This is the behavior I want but I don;t know why xsd.exe is generating this line or how to turn it off.

    Makes sense?



    1) Here's an extract of the XSD:
      <xs:element name="CNSClient">
        <xs:complexType>
          <xs:attribute name="GUID" type="xs:string" use="required" />
          <xs:attribute name="IsActive" type="xs:boolean" use="required" />
        </xs:complexType>
      </xs:element>
    
    


    2) Then, the CS generated by xsd.exe:
    /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://cns.cloudapp.net/xsd")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace="http://cns.cloudapp.net/xsd", IsNullable=false)]
        public partial class CNSClient {
            
            private string gUIDField;
    



    3) Now the code defining the web service:
    [WebMethod]
    
    public CNSWebServiceResult UpdateClient(CNSClient Client, CNSUser UserRequestingAction)
    
    { return CNSClientServiceHelper.UpdateClient(Client, UserRequestingAction); }
    
    
    


    4) And the code generated by the Silverlight client after "Add Web Reference":
    public void UpdateClientAsync(ErcAutoFormTest12Apr09.CNSWebService.Client Client, ErcAutoFormTest12Apr09.CNSWebService.UserRequestingAction UserRequestingAction, object userState) {
    



    ercercerc
    Tuesday, April 28, 2009 2:48 AM
  • Ok, but what is the actual problem? I don't see any extra classes.
    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Tuesday, April 28, 2009 3:18 PM
    Moderator

  • If you look at this line generated on the Silverlight client side (4) notice a class "ErcAutoFormTest12Apr09.CNSWebService.Client" which is not specified in the xsd (1) nor the service definition code (3) nor the cs file generated by xsd.exe (2). The name "Client" in that class is taken from the parameter name, not the parameter class in the service definition (3). If I turn off AnonymousType flag on (2) then the code (4) properly uses the class CNSClient which I defined.

    Since the name of this extra class is taken from teh parameter name you can imagine I have several web methods that use the same parameter name and the extra classes generated clash on these names.

    Improper line:   public void UpdateClientAsync(ErcAutoFormTest12Apr09.CNSWebService.Client Client, ErcAutoFormTest12Apr09.CNSWebService.UserRequestingAction UserRequestingAction, object userState) {

    Proper line:   public void UpdateClientAsync(ErcAutoFormTest12Apr09.CNSWebService.CNSClient Client, ErcAutoFormTest12Apr09.CNSWebService.UserRequestingAction UserRequestingAction, object userState) {


    So, the bottom line is that I need to turn the AnonymousType flag off. Right now I have to do it manually after running xsd.exe. I would feel better if I knew how to turn it off automatically.

    ercercerc
    Wednesday, April 29, 2009 2:44 AM
  • I see now. What I didn't get is that you had to turn it off on the server to prevent conflicts in the client.

    There is no way to change this, as far as I know. You'll have to change the schema to not use an anonymous type:

    <xs:element name="CNSClient" type="tns:CNSClientType"/>
    <xs:complexType name="CNSClientType">
      <xs:attribute name="GUID" type="xs:string" use="required" />
      <xs:attribute name="IsActive" type="xs:boolean" use="required" />
    </xs:complexType>
    Without this change, no matter what you do, XSD.EXE will need to invent a type name.
    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    • Marked as answer by ercercerc Wednesday, April 29, 2009 7:26 PM
    Wednesday, April 29, 2009 6:55 PM
    Moderator

  • Thanks much for the time you invested on this. I will try your suggestion of a change on the XSD.

    ercercerc
    Wednesday, April 29, 2009 7:26 PM