Kuttan Sujith
Kuttan Sujith

Reputation: 7979

Convert xml string to a list of my Object

<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

Upvotes: 9

Views: 21900

Answers (5)

Sreekanth
Sreekanth

Reputation: 395

This approach can be a safer one to avoid exceptions in case if xml result becomes empty from the server call.

string xmlString = "<School><Student><Id>2</Id><Name>dummy</Name><Section>12</Section></Student><Student><Id>3</Id><Name>dummy</Name><Section>11</Section></Student></School>";

XDocument doc = new XDocument();
//Check for empty string.
if (!string.IsNullOrEmpty(xmlString))
{
   doc = XDocument.Parse(xmlString);
}
List<Student> students = new List<Student>();
//Check if xml has any elements 
if(!string.IsNullOrEmpty(xmlString) && doc.Root.Elements().Any())
{
    students = doc.Descendants("Student").Select(d =>
    new Student
     {
       id=d.Element("Id").Value,
      name=d.Element("Name").Value,
      section=d.Element("Section").Value

     }).ToList();   
}
public class Student{public string id; public string name; public string section;}

Check the demo fiddle Demo

Upvotes: 1

roybalderama
roybalderama

Reputation: 1640

Try this one:

var xml = @"    
<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>";

var xElems = XDocument.Parse(xmlString);
var xRow = doc.Root.Elements("row");
List<Row> rowList = (from rowTags in xRow
                     let clickCount = 0
                     let isClickCountOk = Int32.TryParse((rowTags.Element("clickedcount").Value, clickCount);
                     where !string.IsNullOrEmpty(rowTags.Element("linksclicked").Value) &&
                           !string.IsNullOrEmpty(rowTags.Element("clickedcount").Value)
                     select new Row()
                     {
                         linksclicked = rowTags.Element("linksclicked").Value,
                         clickedcount = clickCount 
                     }).ToList();

Upvotes: -1

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

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();

Upvotes: 15

Akanksha Gaur
Akanksha Gaur

Reputation: 2676

You can use an XMLSerializer to deserialize the a property of type List and assign the tag name to the property as root.

However, you'll need the XML tag at the starting of the file.

U can write your own XML parser using XElement. You can the list of row nodes, parse each of them n load them in a list.

XElement rootNode = XElement.Load(filepath);

List<XElement> nodes = rootNode.Descendants().Where(t=> t.Name.ToString().Equals("row"));

for each tag you can create an object Row and fill its properties based on child tags. Hope it helps

Upvotes: 0

Darshan
Darshan

Reputation: 535

You can try this piece of code

string xml = "<Ids><id>1</id><id>2</id></Ids>";   
ArrayList list = new ArrayList();    
XmlDocument doc = new XmlDocument();  
doc.LoadXml(xml);    
XmlNodeList idNodes = doc.SelectNodes("Ids/id");  
foreach (XmlNode node in idNodes)
    list.Add(node.InnerText);

Upvotes: -1

Related Questions