我从thirdParty webApi获得以下响应xml。我想从Xml获取订单ID并保存到我的数据库,我需要其他标签,如消息错误,以显示给用户。所以我想反序列化下面给出的Responsed 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>
答案 0 :(得分:1)
我假设您已经将XML作为服务中的Stream,因此请忽略我的代码以获取Stream,但这是一个用于演示的控制台应用程序。我通过将xml复制到剪贴板并使用Edit&gt;在Visual Studio中粘贴来生成OrderResult
和OrderResultError
类。选择性粘贴&gt;将XML粘贴为类
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;
}
}
}
}