求教 用LINQ查询XML 的方式.
请教高手关于LINQ用法 以下方法GetData()应该返回什么类才正确
〈books>
<book>
<id>0 </id>
<name>ASP.NET AJAX </name>
</book>
....
</books>
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource=GetData();
}
private ******* GetData()
{
XElement xEle=XElement.Load("...");
var books=from x in xEle.Element("books").Elements("book")
select new
{
id=x.Element("id").value,
name=x.Element("name").value,
};
return books;
}
------解决方案--------------------
给Book 类做字段的映射,xml可以与class进行转换
C# code
/**//// <summary>
18 /// 将XML文档转换成实例对象
19 /// </summary>
20 /// <typeparam name="T">对象类型</typeparam>
21 /// <param name="pmFileName">文件名</param>
22 /// <returns>实例对象</returns>
23 public static T DeserializeFile<T>(string pmFileName)
24 {
25 FileStream fs = new FileStream(pmFileName, FileMode.Open);
26 XmlSerializer xs = new XmlSerializer(typeof(T));
27 T tObjext = (T)xs.Deserialize(fs);
28 fs.Close();
29 fs.Dispose();
30 return tObjext;
31 }