简体   繁体   English

在C#中解析XML字符串

[英]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. 我已经查看了同一主题的其他帖子并搜索了Google,但我对C#NET非常陌生并且不知所措。 I am trying to parse this XML... 我试图解析这个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. 我打印出了strResponse的内容,XML绝对存在。 However, when I click this button, nothing is added to the listBox1 or the label1 so I something is wrong. 但是,当我单击此按钮时,没有任何内容添加到listBox1或label1所以我有些错误。

在此处添加Root以开始从根元素( whmcsapi )导航:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM