【发布时间】: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