C# 如何从我的SOAP响应中删除额外的结果标记
我知道以前有人问过这个问题,但我在任何地方都找不到答案 问题是我的asmx文件中有以下代码:C# 如何从我的SOAP响应中删除额外的结果标记,c#,.net,xml,soap,asmx,C#,.net,Xml,Soap,Asmx,我知道以前有人问过这个问题,但我在任何地方都找不到答案 问题是我的asmx文件中有以下代码: namespace IrancellSmsServer { [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)] [WebService(Namespace = "http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")]
namespace IrancellSmsServer
{
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[WebService(Namespace = "http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class SoapServer : System.Web.Services.WebService
{
[WebMethod]
public syncOrderRelationResponse syncOrderRelation(
Sync.UserID userID,
string spID,
string productID,
string serviceID,
string serviceList,
int updateType,
string updateTime,
string updateDesc,
string effectiveTime,
string expiryTime,
item[] extensionInfo
)
{
syncOrderRelationResponse a = new syncOrderRelationResponse();
a.result = 0;
a.resultDescription = "OK";
return a;
}
}
}
这就是结果:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<syncOrderRelationResponse xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
<syncOrderRelationResult> //dont want this
<result>0</result>
<resultDescription>OK</resultDescription>
</syncOrderRelationResult> //dont want this
</syncOrderRelationResponse>
</soap:Body>
</soap:Envelope
//我不要这个
0
好啊
//我不要这个
无法删除此结果。至少没有记录在案
但是,您可以通过返回属性修改此名称:
我知道这可能对您没有直接的帮助(因为您希望将结构展平),但由于这不是一种真正的标准方法,因此至少可以修改结果标记的名称,这会很有帮助。将此添加到您的方法之上
[WebMethod]
[return: XmlElement("syncOrderRelationResponse")]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
[WebMethod]
[return: XmlElement("syncOrderRelationResponse")]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
我也有同样的要求,在挖掘了一段时间后,我找到了如下解决方法:
如前所述,要删除结果标记,请执行以下操作:
[WebMethod]
[return: XmlElement("syncOrderRelationResponse")]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
//the syncOrderRelation method goes here ...
这将产生如下输出:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<syncOrderRelationResponse
xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
<result>0</result>
<resultDescription>OK</resultDescription>
</syncOrderRelationResponse>
</soap:Body>
</soap:Envelope
0
好啊
如果要将
更改为
,只需将[XmlRoot(“carResult”)]添加到对象中,而不是函数/方法中。以下是示例:
[XmlRoot(“carResult”)]
公共类目标车
{
公共字符串模型;
公共字符串颜色;
}
或者将[return:System.Xml.Serialization.xmlementAttribute(“carResult”)]添加到函数/方法中:
[网络方法]
[返回:System.Xml.Serialization.xmlementAttribute(“carResult”)]
公共字符串syncOrderRelation(字符串a)
{
返回a;
}
如果要将
更改为
,只需在函数/方法中添加[SoapDocumentMethodAttribute(ResponseElementName=“YourFunctionName”)]:
[网络方法]
[return:System.Xml.Serialization.xmlementAttribute(“carResult”)][SoapDocumentMethodAttribute(ResponseElementName=“YourFunctionName”)]
公共字符串syncOrderRelation(字符串a)
{
返回a;
}
希望对实现SoapCore(如wcf)有所帮助
就我而言
写这个
[MessageContract(IsWrapped = false)]
返回班长并写下
[return: XmlElement("availabilityResponse")]
要创建适配器类头,请将其添加到方法顶部
[WebMethod]
[return: XmlElement("syncOrderRelationResponse")]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
[WebMethod]
[return: XmlElement("syncOrderRelationResponse")]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
并将其从:
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
致:
你也可以使用这个链接:哇
你为什么不想要它?是什么问题导致您的syncOrderRelationResponse代码也可能有帮助,因为我的客户需要它。的post代码syncOrderRelationResponse@Viru我添加了代码您使用的是XmlSerializer还是DataContractSerializer?这在我使用DataContractSerializer时不起作用,它导致XmlSerializer出错。将[System.ServiceModel.MessageBodyMember]
添加到我的请求/响应对象的属性中修复了该错误,但随后根元素成为我的请求/响应对象的第一个属性。有关详细说明,请参阅。我记得它应该是datacontract Serializer
[WebServiceBinding(ConformsTo = WsiProfiles.None)]