[英]How to create class and deserialize the returned xml response from thirdParty webApi?
I am having the following response xml from the thirdParty webApi. 我从thirdParty webApi获得以下响应xml。 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.
我想从Xml获取订单ID并保存到我的数据库中,我需要其他标签(例如消息Error)显示给用户。 So i want to de-serialize the Responsed xml which is given below.
所以我想反序列化下面给出的xml。 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. 我假设您已经将XML作为服务中的流,因此无需考虑获取该Stream的代码,但这是一个演示控制台的应用程序。 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 我通过将xml复制到剪贴板并在Visual Studio中使用“编辑”>“选择性粘贴”>“将XML粘贴为类”来生成
OrderResult
和OrderResultError
类。
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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.