【问题标题】:WCF XML formatted with Attribute from dictionary使用字典中的属性格式化的 WCF XML
【发布时间】:2014-11-06 22:25:35
【问题描述】:

所以我有一个使用soap 请求返回数据的wcf 服务。此数据以 xml 格式返回。我想要做的是为某个类添加属性到返回的 xml。

例如

public class CustomClass
{
   private string key = "testKey";
   private string value = "100";
}

将返回为

<CustomClass key="testKey">100</CustomClass>

我一直在环顾四周,但找不到可靠的答案。

【问题讨论】:

    标签: c# .net xml wcf


    【解决方案1】:

    您需要绕过 DataContractSerializer 来执行此操作。要么在 CustomClass 上实现 IXmlSerializable 以完全自己控制序列化,要么在您的服务或操作合同上使用 XmlSerializerFormatAttribute 来使用 XmlSerializer,它将尊重您的类属性上的 XmlAttribute 属性。

    How can you control .NET DataContract serialization so it uses XML attributes instead of elements?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多