2009-12-18 55 views
1

我有一个使用xsd.exe工具创建的对象,该工具在代码中定义了xml属性,但来自我的web服务的SOAP响应正在返回xmlelements而不是属性。WCF:SOAP输出主体包含标记名称而不是元素

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
[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 Accountinfo { 

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

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

... etc 

正如你所看到的,UpdatedDate等被定义为属性。 当我打电话给我的服务,皂体,我回去返回Accountinfo元素,例如:

<a:Accountinfo> <a:UpdatedBy>IGD</a:UpdatedBy> <a:UpdatedDate>12/18/2009 9:43:06 AM</a:UpdatedDate> ...等

我在寻找什么是<AccountInfo UpdatedBy="IGD" UpdatedDate="12/18/2009 9:43:06 AM" ... />

我没有太多有关XML,SOAP或WCF的经验,但我现在正在使用这三者,并且需要这些工作。我在这里错过了什么?

回答

2

您的WCF SOAP webservice是否使用标准WCF DataContractSerializer?如果是这样 - 那是预期的行为。为了提高约10%的速度,DataContractSerializer(在WCF中默认使用,除非您明确要求XmlSerializer)放弃XML属性并将所有内容作为元素序列化。

即使您的数据具有所有这些[XmlAttribute]属性也没有什么区别 - 您需要特别要求XmlSerializer,无论是在创建代理客户端时(使用svcutil.exe还是Visual Studio的“添加服务引用”对话框),或者通过在您的服务实现上指定[XmlSerializerFormat]属性。

了解更多关于XmlSerializerFormat attribute看丹Rigsby的的WCF DataContractSerializer vs. XmlSerializer

比较优秀
相关问题