从XML文件获取数据到List()时List.count = 0

时间:2013-10-21 16:43:38

标签: c# xml linq-to-xml

我有一个user.xml文件,如:

<?xml version="1.0" encoding="utf-8"?>
<myXML>
  <RealName>Nguyen Van A</RealName>
  <Email>vyclarks@gmail.com</Email>
  <Phone>2165421</Phone>
  <Address>Ho Chi Minh</Address>
  <Link1>dtvt</Link1>
  <Link2></Link2>
  <Link3></Link3>
</myXML>

我有代码从xml文件中获取数据:

public class user
    {
        public string realname { get; set; }
        public string email { get; set; }
        public string phone { get; set; }
        public string address { get; set; }
        public string link1 { get; set; }
        public string link2 { get; set; }
        public string link3 { get; set; }
    }

public void getUser()
    {
            List<user> us = (
                      from e in
                      XDocument.Load("user.xml").Root.Elements("myXML")
                      select new user
                      {
                        realname = (string)e.Element("RealName"),
                        email = (string)e.Element("Email"),
                        phone = (string)e.Element("Phone"),
                        address = (string)e.Element("Address"),
                        link1 = (string)e.Element("Link1"),
                        link2 = (string)e.Element("Link2"),
                        link3 = (string)e.Element("Link3")
                      }
                      ).ToList();
//get data from list to label

            lblrealname.Text = us[0].ToString();
            lblmail.Text = us[1].ToString();
            lblphone.Text = us[2].ToString();
            lbladd.Text = us[3].ToString();
            lbllink1.Text = us[4].ToString();
            lbllink2.Text = us[5].ToString();
            lbllink3.Text = us[6].ToString();

    }

但是当我构建时,它有一个错误:lblrealname.Text = profiles[0].ToString();这是内容:Index was out of range. Must be non-negative and less than the size of the collection.

然后我调试us.count = 0 ---&gt;我想知道上面的代码是否有错误,所以列表us中没有项目。

帮助!!我找不到哪里出错,是否有更好的方法从该xml文件中获取数据。

2 个答案:

答案 0 :(得分:1)

以下一行

from e in XDocument.Load("user.xml").Root.Elements("myXML")

导致0结果,因为<myXML> 是根,并且它看起来不包含任何嵌套的<myXML>


以下情况应该有效。

from e in XDocument.Load("user.xml").Elements("myXML")

如果您不需要可枚举,则可以使用根

var element = XDocument.Load("user.xml").Root;

答案 1 :(得分:1)

拆分UI和数据访问逻辑。使您的方法返回User对象而不是无效。因此,xml中只有一个根,您不需要编写返回用户列表的查询 - 只需直接访问根节点即可。还可以使用PascalCase作为类型名称,方法和属性:

public User GetUser()
{
    var root = XDocument.Load("user.xml").Root;
    return new User {
         RealName = (string)root.Element("RealName"),
         Email = (string)root.Element("Email"),
         Phone = (string)root.Element("Phone"),
         Address = (string)root.Element("Address"),
         Link1 = (string)root.Element("Link1"),
         Link2 = (string)root.Element("Link2"),
         Link3 = (string)root.Element("Link3")
    };
}

在UI上显示用户:

public void DisplayUser(User user)
{
    lblrealname.Text = user.RealName;
    lblmail.Text = user.Email;
    lblphone.Text = user.Phone;
    lbladd.Text = user.Address;
    lbllink1.Text = user.Link1;
    lbllink2.Text = user.Link2;
    lbllink3.Text = user.Link3;
}

所有在一起:

var user = GetUser();
DispalylUser(user);