user3856437
user3856437

Reputation: 2377

How to Convert the Following Xml to C# Object

<Reports>
   <Report>
       <data Id="2018" type="4" />
       <ReportAddendum>
          <data raID="876" ratext="Text here 1." />
       </ReportAddendum>
       <ReportAddendum>
          <data raID="134" ratext="Text here 2." />
       </ReportAddendum>
       <ReportAddendum>
          <data raID="552" ratext="sfdsfsd" />
       </ReportAddendum>
</Report>

Previously, there's no <ReportAddendum> so I created the following:

[XmlRoot(ElementName = "Reports")]
public class ClassReport
{
    [XmlArray("Report")]
    [XmlArrayItem("data")]
    public List<ReportList> Reports { get; set; }
}

public class ReportList
{
    [XmlAttribute]
    public string Id { get; set; }
    [XmlAttribute]
    public string type { get; set; }
}

Everything works fine. But the issue is the addendum which was added later on, is not being converted even if I add

[XmlRoot(ElementName = "Reports")]
public class ClassReport
{
    [XmlArray("Report")]
    [XmlArrayItem("data")]
    public List<ReportList> Reports { get; set; }

    [XmlArray("ReportAddendum")]
    [XmlArrayItem("data")]
    public List<AddendumList> AddendumList { get; set; }
}

then create a class similar to ReportList.

Upvotes: 0

Views: 65

Answers (1)

Krishna Varma
Krishna Varma

Reputation: 4250

Use xmltocsharp to convert the XML to C# models

[XmlRoot(ElementName = "data")]
public class Data
{
    [XmlAttribute(AttributeName = "raID")]
    public string RaID { get; set; }
    [XmlAttribute(AttributeName = "ratext")]
    public string Ratext { get; set; }
}

[XmlRoot(ElementName = "ReportAddendum")]
public class ReportAddendum
{
    [XmlElement(ElementName = "data")]
    public Data Data { get; set; }
}

public class ReportList
{
    [XmlAttribute]
    public string Id { get; set; }
    [XmlAttribute(AttributeName = "type")]
    public string Type { get; set; }
}

[XmlRoot(ElementName = "Report")]
public class Report
{
    [XmlElement(ElementName = "data")]
    public ReportList Data { get; set; }

    [XmlElement(ElementName = "ReportAddendum")]
    public List<ReportAddendum> ReportAddendum { get; set; }
}

[XmlRoot(ElementName = "Reports")]
public class Reports
{
    [XmlElement(ElementName = "Report")]
    public Report Report { get; set; }
}

then, use XmlSerializer to convert the xml to C# object

using (StreamReader r = new StreamReader(xmlFile))
{
    string xmlString = r.ReadToEnd();

    XmlSerializer ser = new XmlSerializer(typeof(Reports));

    using (TextReader reader = new StringReader(xmlString))
    {
        var reports = (Reports)ser.Deserialize(reader);
        Console.WriteLine($"{reports.Report.Data.Id}:{reports.Report.Data.Type}");
        foreach (var reportAddendum in reports.Report.ReportAddendum)
        {
            Console.WriteLine($"{reportAddendum.Data.RaID}:{reportAddendum.Data.Ratext}");
        }
    }
}

out put

2018:4
876:Text here 1.
134:Text here 2.
552:sfdsfsd

Upvotes: 2

Related Questions