我从改装休息服务(WorkflowMax API)返回了此XML:
<Response api-method=\"Current\"><Status>OK</Status><Jobs><Job><ID>J000002</ID><Name>Callout</Name><Description></Description><Client><ID>20970938</ID><Name>Big Co</Name></Client><ClientOrderNumber></ClientOrderNumber><State>Planned</State><StartDate>2019-04-25T00:00:00</StartDate><DueDate>2019-06-01T00:00:00</DueDate><Contact><ID>12918947</ID><Name>A Smith</Name></Contact><InternalID>34476268</InternalID><Manager><ID>787929</ID><Name>T Smith</Name></Manager><Partner><ID>787929</ID><Name>T Smith</Name></Partner><Assigned><Staff><ID>787929</ID><Name>T Smith</Name></Staff></Assigned></Job><Job><ID>J000003</ID><Name>Call </Name><Description></Description><Client><ID>20982774</ID><Name>Test Group</Name></Client><ClientOrderNumber></ClientOrderNumber><State>Planned</State><StartDate>2019-04-25T00:00:00</StartDate><DueDate>2019-04-25T00:00:00</DueDate><Contact><ID>12924082</ID><Name>S Smith</Name></Contact><InternalID>34476286</InternalID><Partner><ID>787929</ID><Name>T Smith</Name></Partner><Assigned><Staff><ID>787929</ID><Name>T Smith</Name></Staff></Assigned></Job></Jobs></Response>
我希望转换为多个C#Job对象。有人可以建议一种简单的方法吗?
尝试使用Visual Studio粘贴特殊的“将XML作为类粘贴”,并仅粘贴XML的单个“作业”部分,它会生成非常复杂的类,然后不确定如何反序列化为它随后生成的类 >
答案 0 :(得分:0)
使用此便捷的网站生成简单的类,然后可以在以后使用这些类将XML序列化或反序列化为:https://xmltocsharp.azurewebsites.net/
我将利用内置的XML序列化器类对您的类/ XML进行序列化或反序列化:https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-4.8
一些示例代码:
using (TextReader reader = new StreamReader(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(Job));
return (Job)serializer.Deserialize(reader);
}