【问题标题】:WCF XML serialization message formatWCF XML 序列化消息格式
【发布时间】:2020-11-18 20:43:12
【问题描述】:

我希望收到以下格式的输入消息:

<abc:message xmlns:abc="http://www.example.com" specver="0730">
    <abc:myrequest msgid="0123">
        <abc:AttID>3</abc:AttID>
        <abc:AuthNb>100</abc:AuthNb>

为此,我编写了以下代码:

界面

 [ServiceContract(Namespace = "http://www.example.com")]
    [XmlSerializerFormat]
    public interface IService1
    {

        [OperationContract(Name = "message")]
        string RequestData(MyMessageRequest message);

数据合同

    [XmlRoot(Namespace = "http://www.example.com")]
    public class MyMessageRequest
    {

        [XmlAttribute(Namespace = "http://www.example.com", AttributeName = "specver")]
        public string Version { get; set; }

        [XmlElement(ElementName = "myrequest")]
        public MyRequest MyRequest { get; set; }

    }

    [XmlRoot(Namespace = "http://www.example.com")]
    public class MyRequest
    {
        
        [XmlAttribute(AttributeName = "msgid")]
        public string MsgId { get; set; }

        [XmlElement(ElementName = "AttID")]
        public string AttributionID { get; set; }

        [XmlElement(ElementName = "AuthNb")]
        public int AuthenticationNumber { get; set; }

当我将 WSDL 导入 SoapUI 时,我收到以下预定义请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eft="http://www.eftpos2000.ch" xmlns:exam="http://www.example.com">
   <soapenv:Header/>
   <soapenv:Body>
      <eft:message>
         <!--Optional:-->
         <eft:message specver="?">
            <!--Optional:-->
            <exam:myrequest msgnum="?">
               <!--Optional:-->
               <exam:AcqID>?</exam:AcqID>
               <exam:AmtAuth>?</exam:AmtAuth>

您会注意到,我有两次名为“消息”的元素。

任何想法如何删除 XML 根元素“消息”?

BR 尼古拉斯

【问题讨论】:

  • 以下获取一个级别:[XmlElement(ElementName = "myrequest")] 要获得两个级别,请替换为:[XmlArray("myrequest")] 和 [XmlArrayItem("myrequest")]
  • 我不需要任何数组。我想去掉一个“myrequest”元素。
  • 你的 SOAPUI 是否要改变它有两个元素?数组的 XML 序列化 (public MyRequest[] MyRequest { get; set; }) 有两种选择 1) 有一个 xml 元素并使用 XmlElement 2) 有两个 xml 元素并使用 XmlArray 和 XmlArrayItem。现在您的 SOAP 有两个 xml 元素,但您说您只想有一个。你不能同时支持这两个选项。
  • 是的,完全正确:消息中只有一个元素。所以我想不需要声明一个数组。

标签: xml wcf serialization format message


【解决方案1】:

这是因为你方法中的变量名是myrequest,第一个myrequest是变量方法名称:

我修改为message,下图是SoapUI中要求的请求格式:

这是 MyMessageRequest 类:

[XmlRoot(Namespace = "http://www.example.com")]
public class MyMessageRequest
{

    [XmlAttribute(Namespace = "http://www.example.com",AttributeName = "specver")]
    public string Version { get; set; }

    [XmlElement(ElementName = "myrequest")]
    public MyRequest MyRequest { get; set; }

}

更新

第一个消息是操作合约封装的节点。可以删除,但是会生成一个默认方法名的节点,这个不能删除。

【讨论】:

  • 谢谢!我验证了您的方法,但是我也希望删除第一个根元素“消息”。我正在编辑我的初始帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多