I am having the following response xml from the thirdParty webApi. i want to get the Order Id from the Xml and save in to my Database and i need other tags like message Error to show to the User. So i want to de-serialize the Responsed xml which is given below. since i am new to this. Please can you help me to de-serialize the following response. Since i don't have a clear idea about it to create a Class for deserialization. Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<OrderResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Status>Success</Status>
<OrderID>159E6B2AE35244DB984384DBD7DC</OrderID>
<Errors>
<Error>
<Code>1000</Code>
<Message> StopType not valid. Submitted TripSheet data has been ignored.</Message>
<Severity>Unknown</Severity>
</Error>
<Error>
<Code>1000</Code>
<Message> StopType not valid. Submitted TripSheet data has been ignored.</Message>
<Severity>Unknown</Severity>
</Error>
</Errors>
</OrderResult>
I assume you will already have the XML as a Stream from the service, so disregard my code for obtaining the Stream, but here is a console app to demonstrate. I generated the OrderResult
and OrderResultError
classes by copying your xml to the clipboard and pasting in Visual Studio with Edit > Paste Special > Paste XML as Classes
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<OrderResult xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<Status>Success</Status>
<OrderID>159E6B2AE35244DB984384DBD7DC</OrderID>
<Errors>
<Error>
<Code>1000</Code>
<Message> StopType not valid. Submitted TripSheet data has been ignored.</Message>
<Severity>Unknown</Severity>
</Error>
<Error>
<Code>1000</Code>
<Message> StopType not valid. Submitted TripSheet data has been ignored.</Message>
<Severity>Unknown</Severity>
</Error>
</Errors>
</OrderResult>";
XDocument doc = XDocument.Parse(xml);
MemoryStream ms = new MemoryStream();
doc.Save(ms);
ms.Position = 0;
XmlSerializer ser = new XmlSerializer(typeof(OrderResult));
var orderresult = ser.Deserialize(ms) as OrderResult;
Console.WriteLine(orderresult.OrderID);
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class OrderResult
{
private string statusField;
private string orderIDField;
private OrderResultError[] errorsField;
/// <remarks/>
public string Status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
/// <remarks/>
public string OrderID
{
get
{
return this.orderIDField;
}
set
{
this.orderIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Error", IsNullable = false)]
public OrderResultError[] Errors
{
get
{
return this.errorsField;
}
set
{
this.errorsField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderResultError
{
private ushort codeField;
private string messageField;
private string severityField;
/// <remarks/>
public ushort Code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
/// <remarks/>
public string Message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
/// <remarks/>
public string Severity
{
get
{
return this.severityField;
}
set
{
this.severityField = value;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.