在VS2008 ASP.NET项目中使用嵌入式文件

时间:2021-12-09 01:40:09

I have an ASP.NET project and want to include an XML file inside the project to store some relatively static data. To do this I selected "Add File" from the solution context menu and picked my XML file. Having added this to my project, I then wanted to load the XML from within code. I tried the following:

我有一个ASP.NET项目,并希望在项目中包含一个XML文件来存储一些相对静态的数据。为此,我从解决方案上下文菜单中选择了“添加文件”并选择了我的XML文件。将此添加到我的项目后,我想从代码中加载XML。我尝试了以下方法:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("MyData.xml");

I also tried:

我也尝试过:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("~/MyData.xml");

But it seems to be looking in the current directory (i.e. my VS2008 directory) and not the project. Am I going about this wrongly? Is there a way to simply reference a resource that’s embedded in the project like this?

但它似乎在查看当前目录(即我的VS2008目录)而不是项目。我错了吗?有没有办法简单地引用项目中嵌入的资源?

2 个解决方案

#1


0  

The tilde '~' in your second attempt gets evaluated when it is part of a file url set in a control property such as HyperLink.NavigateUrl. If it is just part of a string passed to xmlDocument.Load(), it has to be explicitly evaluated using Server.MapPath("~/MyData.xml")

当第二次尝试中的波形符'〜'在控件属性(如HyperLink.NavigateUrl)中设置的文件URL的一部分时,将对其进行评估。如果它只是传递给xmlDocument.Load()的字符串的一部分,则必须使用Server.MapPath(“〜/ MyData.xml”)显式计算它。

Alternatively,

或者,

You could include the file as an embedded resource. Right click the file in your solution and in the Build Action, select Embedded Resource. In the Copy to Output Directory option, select 'Do not copy'

您可以将该文件包含为嵌入式资源。右键单击解决方案中的文件,然后在“构建操作”中选择“嵌入式资源”。在“复制到输出目录”选项中,选择“不复制”

Here is an example of how to read the file:

以下是如何读取文件的示例:

        var assembly = Assembly.GetExecutingAssembly();
        if (assembly != null)
        {
            var stream = assembly.GetManifestResourceStream("RootProjectNamespace.MyData.xml");
            if (stream != null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(stream);
            }
        }

Note, you have to prefix the filename with the default namespace of your project. You can find this by right clicking your project, selecting properties and in the Application tab, the Default namespace is at the top.

注意,您必须在文件名前加上项目的默认命名空间。您可以通过右键单击项目,选择属性来找到它,并在“应用程序”选项卡中,默认命名空间位于顶部。

#2


0  

The steps you followed to add the XML file in your ASP.NET project does not embed in your application it like just another file in your application like our .aspx pages.

在ASP.NET项目中添加XML文件所遵循的步骤不会嵌入到您的应用程序中,就像应用程序中的另一个文件一样,就像我们的.aspx页面一样。

you may try

你可以试试

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/MyData.xml"));

also it would be better keeping such files in App_Data folder

也可以将这些文件保存在App_Data文件夹中

#1


0  

The tilde '~' in your second attempt gets evaluated when it is part of a file url set in a control property such as HyperLink.NavigateUrl. If it is just part of a string passed to xmlDocument.Load(), it has to be explicitly evaluated using Server.MapPath("~/MyData.xml")

当第二次尝试中的波形符'〜'在控件属性(如HyperLink.NavigateUrl)中设置的文件URL的一部分时,将对其进行评估。如果它只是传递给xmlDocument.Load()的字符串的一部分,则必须使用Server.MapPath(“〜/ MyData.xml”)显式计算它。

Alternatively,

或者,

You could include the file as an embedded resource. Right click the file in your solution and in the Build Action, select Embedded Resource. In the Copy to Output Directory option, select 'Do not copy'

您可以将该文件包含为嵌入式资源。右键单击解决方案中的文件,然后在“构建操作”中选择“嵌入式资源”。在“复制到输出目录”选项中,选择“不复制”

Here is an example of how to read the file:

以下是如何读取文件的示例:

        var assembly = Assembly.GetExecutingAssembly();
        if (assembly != null)
        {
            var stream = assembly.GetManifestResourceStream("RootProjectNamespace.MyData.xml");
            if (stream != null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(stream);
            }
        }

Note, you have to prefix the filename with the default namespace of your project. You can find this by right clicking your project, selecting properties and in the Application tab, the Default namespace is at the top.

注意,您必须在文件名前加上项目的默认命名空间。您可以通过右键单击项目,选择属性来找到它,并在“应用程序”选项卡中,默认命名空间位于顶部。

#2


0  

The steps you followed to add the XML file in your ASP.NET project does not embed in your application it like just another file in your application like our .aspx pages.

在ASP.NET项目中添加XML文件所遵循的步骤不会嵌入到您的应用程序中,就像应用程序中的另一个文件一样,就像我们的.aspx页面一样。

you may try

你可以试试

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/MyData.xml"));

also it would be better keeping such files in App_Data folder

也可以将这些文件保存在App_Data文件夹中

相关文章