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

标签 wcf web-services xsd xml-attribute

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

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

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

最佳答案

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

关于WCF Web 服务 xml 序列化 [XmlAttributeAttribute()] 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976605/

相关文章:

xml - 带有冒号的 XML 语法是什么意思?

json - 将 JSON 日期发送到 WCF 服务

wcf - 数据契约(Contract)和数据成员

c# - 创建 SOAP header ?

xsd - targetNamespace 和 xmlns :target? 之间有什么区别

c# - 亚马逊 MWS C# XSD 类生成

c# - 如何将OperationContract应用到接口(interface)中的所有方法

.net - Microsoft 同步框架与 WCF

java - 如何将通用的rest xml 文件读入java 对象?

java - JAX-WS Web 服务和 Spring Security