开发者

Parsing XML String in C#

I have looked over other posts here on the same subject and searched Google but I am extremely new to C# NET and at a loss. I am trying to parse this XML...

<whmcsapi version="4.1.2"> 
 <action>getstaffonline</action> 
 <result>success</result> 
 <totalresults>1</totalresults> 
 <staffonline> 
  <staff> 
   <adminusername>Admin</adminusername> 
   <logintime>2010-03-03 18:29:12</logintime> 
   <ipaddress>127.0.0.1</ipaddress> 
   <lastvisit>2010-03-03 18:30:43</lastvisit> 
  </staff> 
 </staffonline> 
</whmcsapi>

using this code..

    XDocument doc = XDocument.Parse(strResponse);

    var StaffMembers = doc.Descendants("staff").Select(staff => new
    {
        Name = staff.Element("adminusername").Value,
        LoginTime = staff.Element("logintime").Value,
        IPAddress = staff.Element("ipaddress").Value,
        LastVisit = staff.Element("lastvisit").Value,
    }).ToList();

    label1.Text = doc.Element("totalresults").Value;

    foreach (var staff in StaffMembers)
    {
        listBox1.Items.Add(staff.Name);
    }

I have printed out the contents of strResponse and the XML is definitely there. However, when I click this button, nothing is added to the list开发者_StackOverflow中文版Box1 or the label1 so I something is wrong.


Add Root here to start navigating from the root element (whmcsapi):

string label1_Text = doc.Root.Element("totalresults").Value;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜