Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

List of object array from XML

I have a xml that looks like this

<?xml version="1.0" encoding="utf-8" ?> 
<Resorces>
<Resource id="3" name="loreum ipsum" downloadcount="5"></Resource>
<Resource id="2" name="loreum ipsum" downloadcount="9"></Resource>
</Resorces>

I have a class

public class Report
{
    public int ResourceId {get; set; }
    public string ResourceName { get; set; }
    public int DownloadCount { get; set; }
}

I want to convert that xml into list of Report objects

I tried the below code

var resourceList = doc.Descendants("Resorces")
                                    .First()
                                    .Elements("Resource")
                                    .ToList();

I get values like this,

enter image description here

How can I get it as list of objects?

Upvotes: 0

Views: 66

Answers (4)

Gilad Green
Gilad Green

Reputation: 37299

Basically what you are missing is the part where you convert the Xml objects into your defined object of Report. This is how you can do it:

var resourceList = doc.Descendants("Resorces")
                       .First()
                       .Elements("Resource")
                       .Select(element => new Report()
                                  {
                                    ResourceId = (int)element.Attribute("id"),
                                    ResourceName = (string)element.Attribute("name"),
                                    DownloadCount = (int)element.Attribute("downloadcount")
                                  })
                       .ToList();

I kept here the previous linq methods you called to keep it close to the original but as others said you can just get the Elements("Resource") from the doc root

Upvotes: 2

Mostafiz
Mostafiz

Reputation: 7352

You can get by this

XDocument doc = XDocument.Load(xmlpath);

List<Report> resourceList = doc.Descendants("Resorces")
                                    .First()
                                    .Elements("Resource")
                                    .Select(report => new Report()
                                                          {
                                        ResourceId = (int)report.Attribute("id"),
                                        ResourceName = (string)report.Attribute("name"),
                                        DownloadCount = (int)report.Attribute("downloadcount")
                                    }).ToList();

Upvotes: 1

Kalu Singh Rao
Kalu Singh Rao

Reputation: 1697

 XmlDocument newdoc = new XmlDocument();
newdoc.InnerXml = " <?xml version="1.0" encoding="utf-8" ?> 
<Resorces>
<Resource id="3" name="loreum ipsum" downloadcount="5"></Resource>
<Resource id="2" name="loreum ipsum" downloadcount="9"></Resource>
</Resorces>";
List<string> list = new List <string>();
var selectnode = "Resorces/Resource";
var nodes = newdoc.SelectNodes(selectnode);
foreach (XmlNode nod in nodes)
{
    string id = nod["id"].InnerText;
    string name    = nod["name"].InnerText;
    string downloadcount = nod["downloadcount"].InnerText;       
    list.Add(id);
    list.Add(name);
    list.Add(downloadcount);
}
Console.WriteLine(list.Count);

Upvotes: 2

Hari Prasad
Hari Prasad

Reputation: 16956

You could use Elements method to get elements for a given element name.

    XDocument doc = XDocument.Load(filepath);

    var result = doc.Root
       .Elements("Resource")
       .Select(x=> new Report()
                {
                    ResourceId = int.Parse( x.Attribute("id").Value),
                    ResourceName = (string)x.Attribute("name").Value,
                    DownloadCount = int.Parse( x.Attribute("downloadcount").Value)

                })
        .ToList();

Check this Demo

Upvotes: 1

Related Questions