2011-04-28 100 views
1

任何人都可以帮助我从给定的代码中提取数据&将它显示在屏幕上吗?解析XML以提取数据

<?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> 

请帮助.....

+0

[解析xml文件的最佳实践?]的可能的副本(http://stackoverflow.com/questions/55828/best-practices-to-parse-xml-files) – 2011-04-28 14:53:45

+0

看看http://stackoverflow.com /问题/ 55828 /最佳实践对语法分析XML的文件。 – Nik 2011-04-28 14:54:36

回答

0
 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 
     } 
1

首先,创建一个状态类:

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

接下来,使用LINQ XML创建状态的对象列表

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

一旦你的状态对象列表,将它们以任何您喜欢的方式添加到您的应用程序中都是微不足道的。