【问题标题】:WCF Web Service xml serialization [XmlAttributeAttribute()] is ignoredWCF Web 服务 xml 序列化 [XmlAttributeAttribute()] 被忽略
【发布时间】:2012-12-21 01:02:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: wcf web-services xsd xml-attribute


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多