Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert xml string to a list of my Object

Tags:

c#

<root> 
<row> 
  <linksclicked>http://www.examiner.com/</linksclicked> 
  <clickedcount>34</clickedcount>
</row> 
<row> 
  <linksclicked>http://www.sample123.com</linksclicked> 
  <clickedcount>16</clickedcount>
</row> 
<row> 
  <linksclicked>http://www.testing123.com</linksclicked> 
  <clickedcount>14</clickedcount>
</row> 
</root>

I have xml like above in a string and i have class like below

public class Row
{
    public string linksclicked { get; set; }
    public int clickedcount { get; set; }
}

How can i convert the xml string to a list of Row Object

like image 650
Kuttan Sujith Avatar asked Apr 19 '13 05:04

Kuttan Sujith


1 Answers

You can use LINQ to XML:

var doc = XDocument.Parse(xmlString);

var items = (from r in doc.Root.Elements("row")
             select new Row()
                 {
                     linksclicked = (string) r.Element("linksclicked"),
                     clickedcount = (int) r.Element("clickedcount")
                 }).ToList();
like image 105
MarcinJuraszek Avatar answered Oct 04 '22 19:10

MarcinJuraszek