Convert xml string to my object list

<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 as above in the line and I have a class below

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

How to convert xml string to Row Object list

+5
source share
5 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();
+11
source

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);
0
source

XMLSerializer List root.

XML .

U XML- XElement. , n .

XElement rootNode = XElement.Load(filepath);

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

Row . , .

0

, , xml .

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 script Demo

0
source

Try the following:

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

All Articles