我 profile.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>
按照建议,我通过以下代码从该文件中获取数据:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
public class profile
{
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 getProfile()
{
string path = this.Server.MapPath("~/Lecturer/");
string targetPath = path + @"\"+username+"\\profile.xml";
bool isExists = System.IO.Directory.Exists(targetPath);
if(isExists)
{
List<profile> profiles = (
from e in XDocument.Load(targetPath)
.Root.Element("myXML")
select new profile
{
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();
}
...//code to get list value...
}
但它有一个错误:Cannot resolve symbol "select"
有没有更好的方法从profile.xml
文件中获取数据???