1
<?xml version="1.0" encoding="utf-8" ?>
<reportgroups>
  <Reportgroup id="1" name="reportGroup1">
    <report id="1" name="report1" isSheduled="false"></report>
    <report id="2" name="report2"  isSheduled="false"></report>
    <report id="3" name="report3"  isSheduled="false"></report>
  </Reportgroup>
  <Reportgroup id="2" name="reportGrouop2">
    <report id="4" name="report4"  isSheduled="false"></report>
  </Reportgroup>
  <Reportgroup id="3" name="reportGrouop3"></Reportgroup>
</reportgroups>

and i have classes

public class Reportgroup
{
    public int id { get; set; }
    public string name  { get; set; }
}

public class Report
{
    public int id { get; set; }
    public string name { get; set; }
    public bool isSheduled { get; set; }
}

how can i read this xml to a list of Reportgroup object using linq. What is its syntax. Each item in List of Reportgroup should contain list of Report

1 Answer 1

3

Your Reportgroup class seems to be missing a way to associate with the Reports. Anyway, you can read it like this (with a few tweaks to your classes):

public class ReportGroup // CamelCase!!!
{
    public int Id { get; set; }
    public string Name  { get; set; }
    public List<Report> Reports { get; set; } // need to hold the associated reports
}

public class Report
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsSheduled { get; set; }
}

var doc = XDocument.Load("path/to/file.xml");
var reportGroups = doc.Element("reportgroups")
    .Elements("Reportgroup")
    .Select(rg => new ReportGroup
    {
        Id = (int)rg.Attribute("id"),
        Name = (string)rg.Attribute("name"),
        Reports = rg.Elements("report")
            .Select(r => new Report
            {
                Id = (int)r.Attribute("id"),
                Name = (string)r.Attribute("name"),
                IsScheduled = (bool)r.Attribute("isScheduled"),
            }).ToList();
    }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.