locked
VS2010: WCF Service using XML serialization - xsd schema output RRS feed

  • Question

  • User-1143452311 posted

    Hi,

    I have created a WCF web service and generated the xml serialization class with xsd.exe. I have two problems when I request the xsd from the service (http://localhost:59120/Service1.svc?xsd=xsd2). Firstly attributes are returned as elements and secondly the element names are appended with "Field" does anyone know how to fix either of these?

    Attributes are being shown as elements -

            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string Id {
                get {
                    return this.idField;
                }
                set {
                    this.idField = value;
                }
            }



    Snippet from ...service1.svc?xsd=xsd2 showing element names appended with "Field" and "IdField" should be an attribute not an element

    <xs:sequence>
      <xs:element name="addressField" type="xs:string" nillable="true"/>
      <xs:element name="emailIdField" type="xs:string" nillable="true"/>
      <xs:element name="idField" type="xs:string" nillable="true"/>
      <xs:element name="itemsField" type="tns:ArrayOfArrayOfOrdersCustomerItemsItem" nillable="true"/>
      <xs:element name="nameField" type="xs:string" nillable="true"/>
     </xs:sequence>


    C# Class -

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.296
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    // 
    // This source code was auto-generated by xsd, Version=4.0.30319.1.
    // 
    namespace WcfService1 {
        using System.Xml.Serialization;
        
        
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
        public partial class Orders {
            
            private OrdersCustomer[] itemsField;
            
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Customer", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
            public OrdersCustomer[] Items {
                get {
                    return this.itemsField;
                }
                set {
                    this.itemsField = value;
                }
            }
        }
        
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
        public partial class OrdersCustomer {
            
            private string nameField;
            
            private string addressField;
            
            private string emailIdField;
            
            private OrdersCustomerItemsItem[][] itemsField;
            
            private string idField;
            
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
            public string Name {
                get {
                    return this.nameField;
                }
                set {
                    this.nameField = value;
                }
            }
            
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
            public string Address {
                get {
                    return this.addressField;
                }
                set {
                    this.addressField = value;
                }
            }
            
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2)]
            public string EmailId {
                get {
                    return this.emailIdField;
                }
                set {
                    this.emailIdField = value;
                }
            }
            
            /// <remarks/>
            [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
            [System.Xml.Serialization.XmlArrayItemAttribute("Item", typeof(OrdersCustomerItemsItem), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
            public OrdersCustomerItemsItem[][] Items {
                get {
                    return this.itemsField;
                }
                set {
                    this.itemsField = value;
                }
            }
            
            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string Id {
                get {
                    return this.idField;
                }
                set {
                    this.idField = value;
                }
            }
        }
        
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
        public partial class OrdersCustomerItemsItem {
            
            private string idField;
            
            private string nameField;
            
            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string Id {
                get {
                    return this.idField;
                }
                set {
                    this.idField = value;
                }
            }
            
            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string Name {
                get {
                    return this.nameField;
                }
                set {
                    this.nameField = value;
                }
            }
        }
    }
    


    Any help will be greatly appreciated.

    Wednesday, December 19, 2012 5:20 AM

Answers

All replies

  • User-1622227787 posted

    I wonder if you use an option "/f[ields]" when generate xsd with xsd.exe so that it generates fields instead of properties. By default, properties are generated.

    http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.100).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 20, 2012 2:32 AM
  • User-1143452311 posted

    Oak_Silver,

    Thanks for your reply your suggestion has indeed removed the appended "Field" text, thanks!!

    <xs:sequence> 
      <xs:element name="address" type="xs:string" nillable="true"/> 
      <xs:element name="emailId" type="xs:string" nillable="true"/> 
      <xs:element name="id" type="xs:string" nillable="true"/> 
      <xs:element name="items" type="tns:ArrayOfArrayOfOrdersCustomerItemsItem" nillable="true"/> 
      <xs:element name="name" type="xs:string" nillable="true"/> 
     </xs:sequence>

    Sadly my attribute (<xs:element name="id".../>) is still being returned as an element. It's weird that the processor seems to be ignoring the "[System.Xml.Serialization.XmlAttributeAttribute()] "

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id;

     

    Thursday, December 20, 2012 4:53 AM
  • User-1000095884 posted

    Hi,

    Please take a look at responses in this thread.

    Best Regards.

    Thursday, December 20, 2012 10:18 PM
  • User-1143452311 posted

    Hi,

    I believe that I've fixed this by changing the default serializer. From .Net Framwork 3.0 microsoft changed the default serializer to the DataContract serializer, which is different from the XmlSerializer that ASMX uses.

    "Therefore, the XSD generated varies, too - the DataCOntract serializer for example doesn't support XML attributes (for speed reasons)."

    I specified the XmlSerializer in the service interface [Contract] last night and it seems to be producing the expected xsd now. I did this late last night so haven't tested it properly yet and I'm off now until the new year.

    Here are some links to the thread where I found the info I've been looking for -

    http://stackoverflow.com/questions/2592664/schema-generated-from-wcf-web-service-and-asmx-are-different?rq=1

    Blog post comparing the two serializers

    Serialization in WCF

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2012 6:11 AM