我有一个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
---> 我想知道我上面的代码是否有一些错误,所以 list 中没有项目us
。
帮助!!我找不到哪里出了问题,有没有更好的方法从那个 xml 文件中获取数据。