本文介绍了在C#中解析XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了看其他职位在这里就同一主题和谷歌搜索,但我非常新的C#.NET和不知所措。我试图解析这个XML ...

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



我已经打印出来strResponse的内容和XML肯定是有。然而,当我点击这个按钮,没有任何添加到listBox1中或label1的,所以我什么是错的。

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.

推荐答案

添加这里开始从根元素导航( whmcsapi ):

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

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

这篇关于在C#中解析XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:15
查看更多