varshabhat
varshabhat

Reputation: 11

Parse XML to extract data

can anyone help me to extract the data from the given code & display it on the screen?

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
  <created_at>Sun Dec 19 14:19:35 +0000 2010</created_at>
  <id>16497871259383000</id>
  <text>RT</text>
</status>
 .
 .
 .
</statuses>

pls help.....

Upvotes: 1

Views: 2731

Answers (3)

NakedBrunch
NakedBrunch

Reputation: 49423

First, create a Status class:

public class Status
{
   public string created_at { get; set; }
   public string id { get; set; }
   public string text { get; set; }
} 

Next, use Linq To XML to create a List of Status objects

List<Status> statusList = (from status in document.Descendants("status")
                           select new Status()
                            {
                               created_at = status.Element("created_at").Value,
                               id = status.Element("id").Value,
                               text = status.Element("text").Value
                            }).ToList();

Once you have the List of Status objects, it is trivial to add them in any way you like to your app.

Upvotes: 1

Thomas Langston
Thomas Langston

Reputation: 3735

The minimum viable answer to your question.

XDocument doc = XDocument.Load("PurchaseOrder.xml");
Console.WriteLine(doc);

http://msdn.microsoft.com/en-us/library/bb343181.aspx

Upvotes: 0

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

        var document = new XmlDocument();
        document.LoadXml(xmlString);

        XmlNode rootNode = document.DocumentElement;

        foreach(var node in rootNode.ChildNodes)
        {
            //node is your status node. 
            //Now, just get children and pull text for your UI
        }

Upvotes: 0

Related Questions