问题描述
当前响应
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header />
<s:Body>
<CreateCaseResponse xmlns="http://">
<CreateCaseResult>
<ErrorMessage>\n</ErrorMessage>
<Successful>true</Successful>
<CaseNumber></CaseNumber>
<Recno></Recno>
</CreateCaseResult>
</CreateCaseResponse>
</s:Body>
</s:Envelope>
在上面的响应中,CreateCaseResult
没有namespace
,并且与下面的(CaseNumber and Recno)
相比,数据成员prefix
缺少expected response
预期的反应
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<CreateCaseResponse xmlns="">
<CreateCaseResult xmlns:a="" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ErrorDetails i:nil="true"/>
<ErrorMessage></ErrorMessage>
<Successful>true</Successful>
<a:CaseNumber></a:CaseNumber>
<a:Recno></a:Recno>
</CreateCaseResult>
</CreateCaseResponse>
</s:Body>
</s:Envelope>
序列化代码如下-
private static string MakeSerializedWithSoapEnvelope(object obj,string defaultNamespace)
{
var respformat = @"
<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Header />
<s:Body>
{0}
</s:Body>
</s:Envelope>";
var serializer = new XmlSerializer(obj.GetType(),defaultNamespace);
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("",defaultNamespace);
using (var stringWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter,settings))
{
serializer.Serialize(xmlWriter,obj,ns);
return string.Format(respformat,stringWriter.ToString());
}
}
}
此处defaultNamespace
作为``。
请帮助我对代码进行哪些更改以获取desired and expected output/response.
谢谢。
解决方法
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication169
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("s","http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("","http://software-innovation.com/SI.Data");
ns.Add("a","http://schemas.datacontract.org/2004/07/SI.Data.Contracts.WS");
ns.Add("i","http://www.w3.org/2001/XMLSchema-instance");
Envelope envelope = new Envelope();
Body body = new Body();
envelope.Body = body;
CreateCaseResult results = new CreateCaseResult();
body.CreateCaseResult = new List<CreateCaseResult>() { results};
results.ErrorDetails = new Detail() { nil = true };
results.Successful = true;
results.CaseNumber = "20/00239";
results.Recno = "200245";
results.ErrorMessage = "";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME,settings);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
serializer.Serialize(writer,envelope,ns);
}
}
[XmlRoot(ElementName = "Envelope",Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body",Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
}
public class Body
{
[XmlArray(ElementName = "CreateCaseResponse",Namespace = "")]
[XmlArrayItem(ElementName = "CreateCaseResult",Namespace = "")]
public List<CreateCaseResult> CreateCaseResult { get; set; }
}
public class CreateCaseResult
{
public Detail ErrorDetails { get; set; }
public string ErrorMessage { get; set; }
public Boolean Successful { get; set; }
[XmlElement(ElementName = "CaseNumber",Namespace = "http://schemas.datacontract.org/2004/07/SI.Data.Contracts.WS")]
public string CaseNumber { get; set; }
[XmlElement(ElementName = "Recno",Namespace = "http://schemas.datacontract.org/2004/07/SI.Data.Contracts.WS")]
public string Recno { get; set; }
}
public class Detail
{
[XmlAttribute(AttributeName = "nil",Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public Boolean nil { get; set; }
}
}