虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

XML显示出错

| Comments

今天编译一些学习处理XML文档的代码,结果调试出现上图的错误。网络搜索的解决方案如下:

解决的方法是在命令行窗口(开始–运行–cmd)中输入“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i”。这样问题就解决了。发现,在原先在IIS>默认网站>属性>主目录>配置>映射应用程序映射中,比原先多出了许多扩展名,如我就需要运行了.aspx等,这样,在IIS中就注册完成了。
或者点开始–程序–Microsoft Visual Studio .net 2005–Visual Studio Tools–Visual Studio 命令提示行
在里面写aspnet_regiis.exe -i
重新注册IIS

不管用

 示例代码如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.XPath;

public partial class UpdateXML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.ContentType = "txt/xml";//错误的问题就出在这里,注释掉就好了。
        string xmlPath = Request.PhysicalApplicationPath + @"App_Data\Books.xml";
        XmlDocument document = new XmlDocument();
        document.Load(xmlPath);
        XPathNavigator navigator = document.CreateNavigator();
        int count = navigator.Select("/bookstore/book").Count;
        navigator.MoveToChild("bookstore", "");
        navigator.MoveToChild("book", "");
        for (int i = 0; i < count; i++)
        {
            navigator.MoveToChild("price", "");
            double discount = navigator.ValueAsDouble * (.1);
            navigator.CreateAttribute("","discount","",discount.ToString());
            navigator.MoveToParent();
            navigator.MoveToNext();
        }
        navigator.MoveToRoot();
        Response.Write(navigator.OuterXml);
    }
}

另外一段代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.ContentType = "text/xml";//注释掉也不管用,还是出现老问题
        string xmlFilePath = Request.PhysicalApplicationPath + @"App_Data\Authors.xml";
        XmlReader reader = XmlReader.Create(xmlFilePath);
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        XmlSchemaInference schema = new XmlSchemaInference();
        schemaSet = schema.InferSchema(reader);
        foreach (XmlSchema schemaObj in schemaSet.Schemas())
        {
            schemaObj.Write(Response.Output);
        }
    }
}

Comments