站点图标 久久日记本

system.web.httpunhandledexception错误

最近学会用asp.net创建XML,老师叫我们的是创建XML,修改结点,删除节点,更新节点,但是都是互相独立的文件中进行操作的,我把它们放在一个文件中使用代码,这样是可行的。创建如果是多个人的资料,代码如下:

using System;
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.IO;
//using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static bool FilePicDelete(string path)
    {
        bool ret = false;
        System.IO.FileInfo file = new System.IO.FileInfo("myXmlShow.xml");
        if (file.Exists)
        {
            file.Delete();
            ret = true;
        }
        return ret;

    }
    private void CreatXml()
    {
        XElement myxml = new XElement("studentsList",
        new XElement("list",
            new XElement("name", "王二"),
            new XElement("name", "23"),
            new XElement("tel", "136936363636",
                new XAttribute("type", "移动电话")),
            new XElement("tel", "87878787",
                new XAttribute("type", "家庭电话")),

            new XElement("score",
                new XElement("yuwen", "100"),
                new XElement("shuxue", "100"),
                new XElement("yingyu", "100")
                )
                ),
         new XElement("list",
            new XElement("name", "马六"),
            new XElement("age", "22"),
            new XElement("tel", "136935353535",
                new XAttribute("type", "移动电话")),
            new XElement("tel", "56565656",
                new XAttribute("type", "家庭电话")),

            new XElement("score",
                new XElement("yuwen", "99"),
                new XElement("shuxue", "99"),
                new XElement("yingyu", "99")
                )
                )
                );
        try
        {
            myxml.Save(Server.MapPath("myXmlShow.xml"));
            ErrorBox.Text = "创建成功!";
        }

        catch (Exception E)
        {
            ErrorBox.Text = E.Message;
        }
    }
    protected void ButBegin_Click(object sender, EventArgs e)
    {
        CreatXml();
    }
    protected void ButJdudge_Click(object sender, EventArgs e)
    {
        if (!File.Exists(Server.MapPath("myXmlShow.xml")))//如果不存在
        {
            CreatXml();
            if (File.Exists(Server.MapPath("myXmlShow.xml")))
            {
                ErrorBox.Text = "创建成功!";
            }
            else
                ErrorBox.Text = "创建失败,请检查原因!";
        }
        else//如果存在
        {
            ErrorBox.Text = "同名文件已经存在!";
        }
    }
    protected void ButSel_Click(object sender, EventArgs e)//遍历查询
    {
        XElement myXml = XElement.Load(Server.MapPath("myXmlShow.xml"));
        var xmlSelLINQ = from x in myXml.Descendants("list")
                         where (string)x.Element("age") == "22"
                         select x;

        foreach (var item in xmlSelLINQ)
        {
            ErrorBox.Text = item.ToString();
        }
    }
}

注意红色的的尤其是括号和其它标点符号,避免产生错误。

(update:20160323 md貌似不支持颜色诶,这里说的红色是 CreatXml 方法的代码)

!()[http://img.99diary.com/blog/src/201011261555/1.jpg]

检测到有潜在危险的Request.Form值:

!()[http://img.99diary.com/blog/src/201011261555/2.jpg]

原来的句子中使用LINQ查询xml代码是:

    protected void ButSel_Click(object sender, EventArgs e)//遍历查询
    {
        XElement myXml = XElement.Load(Server.MapPath("myXmlShow.xml"));
        var xmlSelLINQ = from x in myXml.Descendants("list")
                         where (string)x.Element("age") == "22"
                         select x;

        foreach (var item in xmlSelLINQ)
        {
            ErrorBox.Text = item.ToString();
        }
    }

很明显是<>不能出现在显示中,自然是这个显示方法错误(见上面红色),查看的一场详细信息中:system.web.httpunhandledexception,查了一下这个问题,
核对了一下老师的代码,这个地方老师写的是:

foreach (var item in xmlSelLINQ)
{
    Response.Write(item.ToString()+"\t");
}

可以看出ErrorBox.Text 和 Response.Write显示的问题,问了网友,若仍然需要用TextBox可以将代码修改如下:

        foreach (var item in xmlSelLINQ)
        {
           ErrorBox.Text =Server.HtmlEncode(item.ToString());
        }

但是这样就恢复正常了,但是当我点击了“XML的LINQ查询”按钮时,显示的为:

&lt;list&gt;   &lt;name&gt;马六&lt;/name&gt;   &lt;age&gt;22&lt;/age&gt;   &lt;tel type=&quot;移动电话&quot;&gt;136935353535&lt;/tel&gt;   &lt;tel type=&quot;家庭电话&quot;&gt;56565656&lt;/tel&gt;   &lt;score&gt;     &lt;yuwen&gt;99&lt;/yuwen&gt;     &lt;shuxue&gt;99&lt;/shuxue&gt;     &lt;yingyu&gt;99&lt;/yingyu&gt;   &lt;/score&gt; &lt;/list&gt;

很明显需要去掉< > 等空格乱码,这时可以使用Replace函数,查询了一下网上的教程:


Replace函数功能: Replace函数一般应用于字符串的替换,Replace可以替换掉一个字符串中的某些特定字符或者子串并返回值。 Replace语法: public string Replace (string oldValue,string newValue) //oldValue表示要替换的字符,后者表示要替换oldValue的所有匹配项的字符 Replace函数用法-C# CODE string str = "www.zhishiku.net"; str = str.Replace("www"," ")//表示将www替换为空串并返回

于是可以把原来的代码改为:

        foreach (var item in xmlSelLINQ)
        {
            string str = Server.HtmlEncode(item.ToString());
            str = str.Replace("&lt;", " ");//表示将&lt;替换为空串并返回
            string newstr = str;
            newstr = str.Replace("&gt;", " ");//表示将&gt;替换为空串并返回
            ErrorBox.Text = newstr;

        }

这样就可以显示出正常的代码了。再去运行原来的程序。及时重复操作,也不会出现上面的那些错误!!!以后可要记好了!!!

退出移动版