【问题标题】:WCF: SOAP output body contains tag names instead of elementsWCF:SOAP 输出正文包含标记名称而不是元素
【发布时间】:2009-12-18 15:39:38
【问题描述】:

我有一个使用 xsd.exe 工具创建的对象,它在代码中定义了 xml 属性,但是来自我的 Web 服务的 SOAP 响应返回的是 xmlelements 而不是属性。

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

我要找的是&lt;AccountInfo UpdatedBy="IGD" UpdatedDate="12/18/2009 9:43:06 AM" ... /&gt;

我没有太多使用 XML、SOAP 或 WCF 的经验,但我现在正在使用所有这三种方法,并且需要让它们正常工作。我在这里错过了什么?

【问题讨论】:

    标签: c# xml wcf soap


    【解决方案1】:

    您的 WCF SOAP Web 服务是否使用标准 WCF DataContractSerializer?如果是这样 - 这是预期的行为。

    为了提高大约 10% 的速度,DataContractSerializer(在 WCF 中默认使用,除非您明确要求 XmlSerializer)放弃 XML 属性并将所有内容序列化为元素。

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

    阅读有关XmlSerializerFormat attribute 的更多信息,并查看 Dan Rigsby 对 WCF DataContractSerializer vs. XmlSerializer 的出色比较

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2012-01-21
      相关资源
      最近更新 更多