Question

I have a WCF web service developed with VS2010 targeting .Net Framework 4 which utilizes a serialization class generated from a xsd schema using xsd.exe.

When I request the xsd from the service (http://localhost:59120/Service1.svc?xsd=xsd2) the element attributes are ignored. E.G. in the schema snippet below -

<xs:element name="id"...>

should be an attribute -

<xs:attribute name="id"...>

Snippet from xsd request -

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

For some reason the statement "[XmlAttributeAttribute()]" in my class is being ignored I've tried changing it to [XmlAttribute()] and [XmlAttributeAttribute("Id")] and removing the line altogether but it makes no difference at all.

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [XmlTypeAttribute(AnonymousType = true)]
    public partial class OrdersCustomer
    {

        /// <remarks/>
        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
        public string Name;

        /// <remarks/>
        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
        public string Address;

        /// <remarks/>
        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 2)]
        public string EmailId;

        /// <remarks/>
        [XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 3)]
        [XmlArrayItemAttribute("Item", typeof(OrdersCustomerItemsItem), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
        public OrdersCustomerItemsItem[][] Items;

        /// <remarks/>
        [XmlAttributeAttribute()]
        public string Id;
    }
Was it helpful?

Solution

Make sure that your [ServiceContract] interface has the [XmlSerializerFormat] attribute. If this is not present, then WCF will use the DataContractSerializer, and all your XmlSerializer attributes will be ignored.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top