如何清除WCF服务响应中不需要的字符
本文关键字:响应 不需要 字符 服务 WCF 何清除 清除 | 更新日期: 2024-10-21 06:11:07
我有一个WCF服务,它以XML格式向客户端发送响应。当我用Soap UI测试它时,它给了我一些额外的字符。在评论中点赞并命名为https://cf.ctr.com:DealerId'那就需要删除它们。我不确定它们是从哪里来的。这是我得到的回应:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ProcessXMLResponse xmlns="https://cf.ctr.com">
<ProcessXMLResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<StatusCode>201</StatusCode>
<StatusDescription>XML Validation Fails</StatusDescription>
<comments xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string/>
<a:string>The element 'Collateral' in namespace 'https://cf.ctr.com' has invalid child element 'VIN' in namespace 'https://cf.ctr.com'. List of possible elements expected: 'Vehicle' in namespace 'https://cf.ctr.com'.</a:string>
<a:string>The 'https://cf.ctr.com:DealerId' element is invalid - The value '104213' is invalid according to its datatype 'UnsignedShort' - The Pattern constraint failed.</a:string>
<a:string>The 'https://https://cf.ctr.com:EmploymentPeriodInMonth' element is invalid - The value '0' is invalid according to its datatype 'Integer' - The MinInclusive constraint failed.</a:string>
<a:string>The 'https://cf.ctr.com:Name' element is invalid - The value '' is invalid according to its datatype 'String' - The actual length is less than the MinLength value.</a:string>
</comments>
<RemoteRefNumber>104213C3C4B606-2A10-46F4-87FA-E23</RemoteRefNumber>
</ProcessXMLResult>
</ProcessXMLResponse>
这是我的合同:
[DataContract(Namespace = "https://https://cf.ctr.com")]
public class PartnerRequest
{
// ------------------------------------------------------------------- Username and Password in SOAP's Header - START
[DataMember(Name = "username",Order=1)]
public string username { get; set; }
[DataMember(Name = "password",Order=2)]
public string password { get; set; }
// ------------------------------------------------------------------- Username and Password in SOAP's Header --- END
// Body
[DataMember(Name = "PartnerID", Order=3)]
public int PartnerID { get; set; }
[DataMember(Name = "PartnerName",Order=4)]
public string PartnerName { get; set; }
[DataMember(EmitDefaultValue = false,Name = "CFCConnect", Order = 5)]
public CFCConnect CFCConnect { get; set; }
}
[DataContract(Namespace = "https://cf.ctr.com")]
public class PartnerAuthentication
{
public PartnerAuthentication()
{
}
public PartnerAuthentication(PartnerAuthentication partner)
{
this.Authenticated = partner.Authenticated;
this.StatusCode = partner.StatusCode;
this.StatusDescription = partner.StatusDescription;
this.comments = partner.comments;
this.RemoteRefNumber = partner.RemoteRefNumber;
}
//[MessageBodyMember(Order = 1, Namespace = "https://cf.ctr.com")]
public bool Authenticated { get; set; }
[DataMember(Name = "StatusCode", Order = 1)]
public int StatusCode { get; set; }
[DataMember(Name = "StatusDescription", Order = 2)]
public string StatusDescription { get; set; }
[DataMember(Name = "comments", Order = 3)]
public List<string> comments { get; set; }
[DataMember(Name = "RemoteRefNumber", Order = 4)]
public string RemoteRefNumber { get; set; }
没有"多余"字符。当您通过将内容反序列化为具体类型或解析架构来反转过程时,可能需要"额外"信息,例如"https://cf.ctr.com:DealerId"是某些架构中的命名空间和属性名。
在我看来,您正在从某种形式的XSD验证引擎获得评论,该引擎将为您提供错误的完整解释,例如"https://cf.ctr.com:DealerId"元素无效-根据其数据类型"UnsignedShort",值"104213"无效-Pattern约束失败。"
将XML作为字符串在网络上传递不是一个好主意。删除它们更为复杂,而且可能是错误的方向。
最后,你为什么担心它在电线上的样子?如果你的伴侣在你的评论中向你发送了一大堆问题,你必须在收到后将其转化为对你有意义的东西,无论它在网上看起来如何。