c# - How can I deserialize an object from an XML returned from a nusoap service?
问题描述
I'm trying to deserialize an object from an Xml which is returned from a NuSoap-Service. I received the Service with a WSDL-File and tried to implement it into my project. The problem is that the WSDL-File seems to broken. I have access to the webservice over SoapUI.I can also do my requests handfilled into an XML-form successfully. In return to a request I get an Xml back from the service.
I already made the classes for some of the requests. I can serialize and post them successfully over and get my Xml-response. Now my actual problem starts. I would like to deserialize this response.
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:LicenseManagement">
<SOAP-ENV:Body>
<ns1:GetErrorCodesResponse xmlns:ns1="urn:LicenseManagement">
<RETURN xsi:type="tns:ReturnGetErrorCodes">
<RC xsi:type="xsd:int">0</RC>
<DATA xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType=":[55]">
<item xsi:type="xsd:string">OK</item>
<item xsi:type="xsd:string">database unavailable</item>
<item xsi:type="xsd:string">...</item>
</DATA>
</RETURN>
</ns1:GetErrorCodesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I started with the implementation of the classes for one of the responses. I could manage to deserialize the Envelope, Body, GetErrorResponse part of this XML. When I reach the RETURN part I get a null.
I know I could use DataSet for my Problem, but that would require a MappingProfile for each response.
try
{
//
GetErrorCodesRequestModel request = new GetErrorCodesRequestModel()
{
Username = "xxx" ,
Password = "zzZzZ"
};
var res = XmlExtension.SerializeToString(request);
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(res);
HttpWebRequest webRequest = CreateWebRequest(command , "POST");
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml , webRequest);
IAsyncResult asyncResult = webRequest.BeginGetResponse(null , null);
asyncResult.AsyncWaitHandle.WaitOne();
string soapResponse = string.Empty;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResponse = rd.ReadToEnd();
}
// this is the part where I have to deserialize the soapResponse
var deserialized = XmlExtension.Deserialize<Envelope>(soapResponse);
// deserialized contains information til RETURN
// important information missing
My classes look like this atm.
[XmlRoot(ElementName = "RC")]
public class RC
{
[XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "item")]
public class Item
{
[XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "DATA")]
public class DATA
{
[XmlElement(ElementName = "item")]
public Item[] Item { get; set; }
[XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "arrayType" , Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string ArrayType { get; set; }
}
[XmlType(AnonymousType = true , IncludeInSchema = true)]
[XmlRoot(ElementName = "RETURN", Namespace = "tns:ReturnGetErrorCodes" , IsNullable = false)]
public class RETURN
{
[XmlElement(ElementName = "RC")]
public RC RC { get; set; }
[XmlElement(ElementName = "DATA")]
public DATA DATA { get; set; }
[XmlAttribute(AttributeName = "type" , Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "GetErrorCodesResponse" , Namespace = "urn:LicenseManagement")]
public class GetErrorCodesResponse
{
[XmlElement(ElementName = "RETURN")]
public RETURN RETURN { get; set; }
[XmlAttribute(AttributeName = "ns1" , Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns1 { get; set; }
}
[XmlRoot(ElementName = "Body" , Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "GetErrorCodesResponse" , Namespace = "urn:LicenseManagement")]
public GetErrorCodesResponse GetErrorCodesResponse { get; set; }
}
[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; }
[XmlAttribute(AttributeName = "SOAP-ENV" , Namespace = "http://www.w3.org/2000/xmlns/")]
public string SOAPENV { get; set; }
[XmlAttribute(AttributeName = "xsd" , Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi" , Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "SOAP-ENC" , Namespace = "http://www.w3.org/2000/xmlns/")]
public string SOAPENC { get; set; }
[XmlAttribute(AttributeName = "tns" , Namespace = "http://www.w3.org/2000/xmlns/")]
public string Tns { get; set; }
}
解决方案
解决这些问题的最佳方法是使用示例数据创建类并进行序列化。然后将您的序列化 xml 与原始 xml 进行比较
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
Envelope envelope = new Envelope() {
Body = new Body() {
GetErrorCodesResponse = new GetErrorCodesResponse() {
Ns1 = "urn:LicenseManagement",
RETURN = new RETURN() {
Type = "tns:ReturnGetErrorCodes",
RC = new RC() {
Type = "xsd:int",
Text = "0"
},
DATA = new DATA() {
Type = "SOAP-ENC:Array",
ArrayType = ":[55]",
Item = new Item[] {
new Item() { Type = "xsd:string", Text = "OK"},
new Item() { Type = "xsd:string", Text = "database unavailable"},
new Item() { Type = "xsd:string", Text = "..."}
}
}
}
}
}
};
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
serializer.Serialize(writer, envelope);
}
}
推荐阅读
- python - 将 Angular 与 django 集成时由 vendor.js 启动的文件出现 404 错误
- angular - 突出显示 html 模板中的角度语法
- reporting-services - 即使详细信息为空,SSRS 2008 也会显示行
- c# - 如何将标题应用于文字处理文档中的所有段落?
- azure - 安装 MS Service Fabric SDK 3.2.162 时出现问题
- schema - Pentaho Schema Workbench 维度属性
- c# - UWP XAML 中的 YouTube 样式搜索结果
- python - ImportError:没有名为 miniconda 的模块
- ag-grid - 将列从像素宽度更改为百分比宽度以响应并在 ag-grid 中填充表格
- blogspot - 如何为 Blogspot 放置最佳元数据?