WCF Web服务xml序列化[XmlAttributeAttribute()]被忽略

时间:2012-12-20 16:37:23

标签: wcf web-services xsd xml-attribute

我有一个使用VS2010开发的WCF Web服务,目标是.Net Framework 4,它使用xsd.exe从xsd架构生成的序列化类。

当我从服务请求xsd时(http:// localhost:59120 / Service1.svc?xsd = xsd2),将忽略元素属性。例如。在下面的架构代码段中 -

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

应该是一个属性 -

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

来自xsd请求的片段 -

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

由于某种原因,我的类中的语句“[XmlAttributeAttribute()]”被忽略我已经尝试将其更改为[XmlAttribute()]和[XmlAttributeAttribute(“Id”)]并完全删除该行,但它使得没有任何区别。

    /// <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;
    }

1 个答案:

答案 0 :(得分:5)

确保您的[ServiceContract]界面具有[XmlSerializerFormat]属性。如果不存在,则WCF将使用DataContractSerializer,您的所有XmlSerializer属性都将被忽略。