...是物理路径,但预计会有虚拟路径。 [英] ...is a physical path, but a virtual path was expected.

查看:90
本文介绍了...是物理路径,但预计会有虚拟路径。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我收到此错误:

Hi I am getting this error:

Read_Insert_Data_Into_XML_File/App_Data/Sample.xml'' is a physical path, but a virtual path was expected.



这是我的代码:


This is my code:

protected void btnSubmit_Click(object sender, EventArgs e)
   {
       XmlDocument xmlDoc = new XmlDocument();
       string xmlPath = Path.Combine(Server.MapPath("~/App_Data"), "Sample.xml");
       if (!File.Exists(xmlPath))
       {
           throw new Exception("Cannot find xml file");
       }
       else
       {
           xmlDoc.Load(Server.MapPath(xmlPath));
           XmlElement parentElement = xmlDoc.CreateElement("Comments");
           XmlElement name = xmlDoc.CreateElement("Name");
           name.InnerText = txtName.Text;
           parentElement.AppendChild(name);
           parentElement.AppendChild(Location);
           xmlDoc.DocumentElement.AppendChild(parentElement);
           xmlDoc.Save(Server.MapPath(xmlPath));
           BindDataList();
    }



我在这行代码中收到错误:

xmlDoc.Load(Server.MapPath (xmlPath));


I am getting an error at this line of code:
xmlDoc.Load(Server.MapPath(xmlPath));

推荐答案

您已经将虚拟路径转换为物理路径,因此第二次尝试转换失败,因为它已经是物理路径:



You already converted the virtual path to a physical path, so the second time you try the conversion fails as it already is a physical path:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDoc = new XmlDocument();
        string xmlPath = Path.Combine(Server.MapPath("~/App_Data"), "Sample.xml"); // The conversion is done here, by Server.MapPath
        if (!File.Exists(xmlPath))
        {
            throw new Exception("Cannot find xml file");
        }
        else
        {
            //xmlDoc.Load(Server.MapPath(xmlPath));   // Error happens here too
            xmlDoc.Load(xmlPath);                     // Same as below 
            XmlElement parentElement = xmlDoc.CreateElement("Comments");
            XmlElement name = xmlDoc.CreateElement("Name");
            name.InnerText = txtName.Text;
            parentElement.AppendChild(name);
            parentElement.AppendChild(Location);
            xmlDoc.DocumentElement.AppendChild(parentElement);
            //xmlDoc.Save(Server.MapPath(xmlPath)); // Error happens here as the path was already a physical path
            xmlDoc.Save(xmlPath);                   // Just leave out the Server.MapPath call
            BindDataList();
     }





问候,



- Manfred


浏览这些链接。它肯定会帮助你。



Server.MapPath - 给定物理路径,预期虚拟路径



从物理路径转换为虚拟路径



将物理路径转换为虚拟路径&反之亦然
Go through these links.It wil definately help you.

Server.MapPath - Physical path given, virtual path expected

Convert from physical path to virtual path

Converting a physical path to Virtual path & vice versa


这篇关于...是物理路径,但预计会有虚拟路径。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆