0
XElement Xml = null;
var apiResponse = response.Content.ReadAsStringAsync().Result;
Xml = Newtonsoft.Json.JsonConvert.DeserializeObject<XElement>(apiResponse);

XML response from the above code:

I'm having errors with Images XML part while converting it to List, I tried so many options, please provide suggestion below:

<root>
  <Column1>
    <ID>2702</ID>
    <Desc>Failed</Desc>
    <Address>Florida</Address>
    <Date>2019-04-30T23:10:36.79</Date>
    <Images>
      <Image>
        <File>1-RRamos.PNG</File>
      </Image>
      <Image>
        <File>RRamos.PNG</File>
      </Image>
      <Image>
        <File>3-RRamos.PNG</File>
      </Image>
    </Images>
  </Column1>
</root>

Trying to convert from XML to List as below:

public class objClass
{
    public string ID { get; set; }
    public string Desc { get; set; }
    public string Address { get; set; }
    public DateTime? Date { get; set; }

    //public string[] ImageFileNames { get; set; }

    public List<Images> FileName { get; set; }
}

public class Images
{
    public string File { get; set; }
}

List<objClass> list = Xml.Elements("ID").Select(sv => new objClass()
{
    ID = (string)sv.Element("ID"),
    Desc = (string)sv.Element("Desc"),
    Address = (string)sv.Element("Address"),
    Date = (DateTime?)sv.Element("Date"),
    //**,Images = (List)sv.Element("Images")**
}).ToList();
Yong Shun
  • 35,286
  • 4
  • 24
  • 46

2 Answers2

1

You can't use the Newtonsoft.Json library to deserialize from an XML.

Solution 1: Convert XML to list via XPath

  1. Parse XML string to XDocument.

  2. With XPath: "//root/Column1", select the <Column1> element.

using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml.XPath;

var apiResponse = await response.Content.ReadAsStringAsync();
XDocument @Xml = XDocument.Parse(apiResponse);
        
List<ObjClass> list = @Xml.XPathSelectElements("//root/Column1")
    .Select(sv => new ObjClass()
            {
                ID = (string)sv.Element("ID"),
                Desc = (string)sv.Element("Desc"),
                Address = (string)sv.Element("Address"),
                Date = (DateTime?)sv.Element("Date"),
                Images = sv.Element("Images")
                    .Elements("Image")
                    .Select(x => new Image
                            {
                                File = (string)x.Element("File")
                            })
                    .ToList()   
            })
    .ToList();

Solution 2: Deserialize XML

This answer will be a bit complex but work the same as Solution 1.

  1. Write the apiResponse value into MemoryStream.

  2. Deserialize the MemoryStream via XmlSerializer as Root.

  3. Extract root.Column and add into list.

[XmlRoot(ElementName = "root")]
public class Root
{
    [XmlElement("Column 1")]
    public ObjClass Column { get; set; }
}

public class ObjClass
{
    public string ID { get; set; }

    public string Desc { get; set; }

    public string Address { get; set; }

    public DateTime? Date { get; set; }

    [XmlArray]
    [XmlArrayItem(typeof(Image), ElementName = "Image")]
    public List<Image> Images { get; set; }
}

public class Image
{
    public string File { get; set; }
}
using System.Xml;
using System.Xml.Serialization;
using System.IO;

var apiResponse = await response.Content.ReadAsStringAsync();

using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
writer.Write(apiResponse);
writer.Flush();
stream.Position = 0;
        
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Root));      
        
Root root = (Root)xmlSerializer.Deserialize(stream);
List<ObjClass> list = new List<ObjClass>();
list.Add(root.Column);

Concern:

  1. Use await instead of Task<T>.Result as Task<T>.Result will block the calling thread until it (the task) is completed. With await, the task is waited to be completed asynchronously. Reference: What is the difference between await Task and Task.Result?
Yong Shun
  • 35,286
  • 4
  • 24
  • 46
0
public class objClass
{
    public string ID { get; set; }
    public string Desc { get; set; }
    public string Address { get; set; }
    public DateTime? Date { get; set; }
    public string[] ImageFileNames { get; set; }
}

var list = Xml.Elements("root").Elements("Column1")
.Select(sv => new objClass()
{
    ID = (string)sv.Element("ID"),
    Desc = (string)sv.Element("Desc"),
    Address = (string)sv.Element("Address"),
    Date = (DateTime?)sv.Element("Date"),
    ImageFileNames = sv.Element("Images")
        .Elements("Image")
        .Select(i => (string)i.Element("File"))
        .ToArray(),
})
.ToList();
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59