12

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 listBox1 or the label1 so I something is wrong.

1 Answer 1

13

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

string label1_Text = doc.Root.Element("totalresults").Value;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that resolved it. After that was fixed I realized no one was online so it wasn't adding anything to the listbox. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.