0
我profile.xml
文件在我的Web文件夾:錯誤時從XML文件中獲取數據
<?xml version="1.0" encoding="utf-8"?>
<myXML>
<RealName>Nguyen Van A</RealName>
<Email>[email protected]</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
文件獲取數據???
測試它,但是當我建成,profiles.count = 0,我不知道這是否是代碼不適合我的XML語法。幫幫我??? –
請參閱更新的答案,我已經用您的XML測試了第二個片段。第一次我錯過了,你已經將所有東西存儲在一個名爲myXML的根元素中。我認爲你可能有多個配置文件存儲在一個XML文件中。 – Perevalov