日期:2014-05-20  浏览次数:20838 次

是MSDN文档出错了,还是作者穿越了,关于NET Framework版本的问题!!!!!!!
今天看一本书,出现了问题,这是作者的源代码,后台代码文件代码如下//程序名称:7-01.aspx.cs
//程序功能:XML树的创建和查询
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;

public partial class _7_01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 创建XML树
/// </summary>
private void CreateXML()
{
XElement contacts = new XElement("学生列表",
new XElement("详细资料",
new XElement("姓名", "王二"),
new XElement("年龄", "23"),
new XElement("电话", "13888888888",
new XAttribute("类型", "移动电话")),
new XElement("电话", "87878787",
new XAttribute("类型", "家长联系电话")),
new XElement("成绩",
new XElement("语文", "88"),
new XElement("历史", "91"),
new XElement("政治", "70")
)
)
);
//保存到xml文件中
contacts.Save(Server.MapPath("students.xml"));
}
protected void btnCreateXML_Click(object sender, EventArgs e)
{
//判断是否已经存在同名文件
//如果不存在
if (!File.Exists(Server.MapPath("students.xml")))
{
//调用此方法进行创建和保存
CreateXML();
//创建后如果存在,则证明创建成功
if (File.Exists(Server.MapPath("students.xml")))
{
//输出成功信息
Response.Write("创建成功!");
}
}
else //如果已存在
{
//输出提示信息
Response.Write("保存失败:同名文件已存在!");
}
}
//查询全部
protected void btnQueryAll_Click(object sender, EventArgs e)
{
//加载xml文件
XElement exl = XElement.Load(Server.MapPath("Product.xml"));
//LINQ查询
var c = from x in exl.Descendants("Product")
select x;
//遍历结果集
foreach (var cc in c)
{
//输出
Response.Write(cc.ToString() + " <br>");
}
}
//带条件查询
protected void btnQuery_Click(object sender, EventArgs e)
{
//首先加载xml文件
XElement exl = XElement.Load(Server.MapPath("Product.xml"));
//条件查询
var c = from x in exl.Descendants("Product")
where (string)x.Element("ProductID") == "1"
select x;
//遍历结果集
foreach (var cc in c)
{
//输出
Response.Write(cc.ToString() + "<br>");
}
}
}
——————————————————————————————————————————————————————————
前台代码文件如下
<%--程序名称:11-04.aspx--%>
<%--程序功能:XML树的创建和查询--%>
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="7-01.aspx.cs" Inherits="_7_01" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>XML树的创建和查询</title>
</head>
<body>
<form id="form1" runat="server">

<asp:Button ID="btnCreateXML" runat="server" Text="创建XML树"
onclick="btnCreateXML_Click" />

<asp:Button ID="btnQueryAll" runat="server" Text="查询全部"
onclick="btnQueryAll_Click" />

<asp:Button ID="btnQuery" runat="server" Text="带条件查询"
onclick="btnQuery_Click" />

</form>
</body>
</html>————————————————————————————————————————————————————

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
运行结果出错,visual studio 2005提示不认识linq缺少空间引用,好吧,查了一下MSDN文档,上面说system.linq的EXlement类(上面代码中用了此类)NET Framework 4.0 我看的这本书出版与2009年