1

I have this XML returned from a Refit rest service (WorkflowMax API):

<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>

I wish to convert to multiple C# Job objects. Can someone please suggest a simple way?

Tried Using visual studio Paste Special 'Paste XML as Classes' and just pasting in a single 'job' part of the XML it produces quite a complex Class and then not sure how to deserialize into the class it then has prodcued

David
  • 19
  • 1
  • 8

1 Answers1

4

Use this handy website to generate simple classes which you can then later on use to serialize or deserialize XML to: https://xmltocsharp.azurewebsites.net/

I would make use of the built-in XML serializer class to serialize or deserialize your classes/XML: https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-4.8

Some sample code:

using (TextReader reader = new StreamReader(path))
{
  XmlSerializer serializer = new XmlSerializer(typeof(Job));
  return (Job)serializer.Deserialize(reader);
}
DaGrooveNL
  • 177
  • 1
  • 1
  • 10
  • Thanks, I had to remove the outer section of the XML first to get the jobs – David Apr 30 '19 at 00:04
  • The correct solution is to create a class RESPONSE and then de-serialize the xml as typeof(response). – jdweng Apr 30 '19 at 01:36
  • Yes ofcourse, Response can be used as an outer class and the XML can then be de-serialized to this class. – DaGrooveNL May 02 '19 at 23:10
  • I'm getting a warning with FXCOP analyzer in VisualStudio with this: Warning CA5369 This overload of the 'XmlSerializer.Deserialize' method is potentially unsafe. It may enable Document Type Definition (DTD) which can be vulnerable to denial of service attacks, or might use an XmlResolver which can be vulnerable to information disclosure. Use an overload that takes a XmlReader instance instead, with DTD processing disabled and no XmlResolver. – Rumplin Nov 17 '20 at 17:57