在 C# 中加载 XML 文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19957369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading an XML file path in C#
提问by Tobias Roland
I'm trying to load an XML-file, located in a folder in my project (using Visual Studio 2012).
我正在尝试加载位于项目文件夹中的 XML 文件(使用 Visual Studio 2012)。
The structure is this:
结构是这样的:
solutionRoot\
- service\
-- ServiceClass.cs
-- AppValues.xml <-- this is the file I want to load
In my ServiceClass, I'm trying to read from the XML-file with the following code:
在我的 ServiceClass 中,我尝试使用以下代码从 XML 文件中读取:
public String GetXmlElement(String elementName)
{
[....]
XDocument document = XDocument.Load(@"\service\AppValues.xml");
[...]
}
Which gives the following error, when I'm trying to test the code:
当我尝试测试代码时,会出现以下错误:
Test method PandaTests.ServiceTest.ReadXmlCanReadXml threw exception:
System.IO.DirectoryNotFoundException: Could not find a part of the path
'C:\Users\MyName\Documents\GitHub\project\Project22\PandaTests\bin\Debug\service\AppValues.xml'.
It's obviously a problem with my path, but I can't figure out how to get the relative path right. I've looked at other questions here on stack overflow, but many of them seem overly involved. Is there an easy way to load the XML-file without giving an absolute path?
这显然是我的路径有问题,但我不知道如何获得正确的相对路径。我在此处查看了有关堆栈溢出的其他问题,但其中许多问题似乎过于复杂。有没有一种简单的方法可以在不提供绝对路径的情况下加载 XML 文件?
采纳答案by Tyler Lee
When VS runs your program, your working directory is set to the Debug/Release folder, not to your solution root.
当 VS 运行您的程序时,您的工作目录将设置为 Debug/Release 文件夹,而不是您的解决方案根目录。
You have a couple options that I know of...
你有几个我知道的选择......
- Use an absolute path, but you don't want this
- Set your file to copy into your working directory on build. You do this by modifying the properties of the file in the solution explorer. Thanks to T.Roland in the comments below: Set Copy to Output Directoryto Copy if Newer and set Build Actionto Embedded Resource;
- Modify your solution's working directory to be your solution root This threadoffers different ways to accomplish that.
- 使用绝对路径,但你不想要这个
- 将您的文件设置为在构建时复制到您的工作目录中。您可以通过在解决方案资源管理器中修改文件的属性来完成此操作。 感谢 T.Roland 在下面的评论中:将Copy to Output Directory设置为 Copy if Newer 并将Build Action设置为 Embedded Resource;
- 将您的解决方案的工作目录修改为您的解决方案根该线程提供了不同的方法来实现这一点。
回答by K3rnel31
check this
检查这个
XDocument document = XDocument.Load(@"..\service\AppValues.xml");
回答by jlew
Bring up the properties in Visual Studio for AppValues.xml. Change "Copy to Output Directory" to "Copy if Newer", and build the project.
在 Visual Studio 中为 AppValues.xml 调出属性。将“复制到输出目录”更改为“如果较新则复制”,然后构建项目。
回答by guymid
Set the build action of the xml file to be "Embedded resource" and then reference using this code
将 xml 文件的构建动作设置为“嵌入式资源”,然后使用此代码引用
private static UnmanagedMemoryStream GetResourceStream(string resName)
{
var assembly = Assembly.GetExecutingAssembly();
var strResources = assembly.GetName().Name + ".g.resources";
var rStream = assembly.GetManifestResourceStream(strResources);
var resourceReader = new ResourceReader(rStream);
var items = resourceReader.OfType<DictionaryEntry>();
var stream = items.First(x => (x.Key as string) == resName.ToLower()).Value;
return (UnmanagedMemoryStream)stream;
}
var file = GetResourceStream("appValues.xml");
回答by Kami
When adding a file to Visual Studio project, by default it is not copied to the generated output. As such, you need to set to either copy the file or do so manually.
将文件添加到 Visual Studio 项目时,默认情况下不会将其复制到生成的输出中。因此,您需要设置为复制文件或手动执行此操作。
To set the file to automatically copy, select it in solution explorer, right click and select properties. Update the value for "Copy to Output Directory" to "Copy Always". This will ensure a copy of the file is available at runtime in a subfolder of the resultant solution.
要将文件设置为自动复制,请在解决方案资源管理器中选择它,右键单击并选择属性。将“复制到输出目录”的值更新为“始终复制”。这将确保文件的副本在运行时在结果解决方案的子文件夹中可用。
You can then load the file using something like:
然后,您可以使用以下内容加载文件:
string path = System.Io.Path.Combine(Application.StartupPath, @"\service\AppValues.xml");
XDocument doc = XDocument.Load(path);
回答by Sarath Rachuri
I faced the same problem and solved it using "Server.MapPath"
我遇到了同样的问题并使用“Server.MapPath”解决了它
For example,
例如,
string path=Server.MapPath("~/service/AppValues.xml");
XDocument document = XDocument.Load(path);
Hope it helps.
希望能帮助到你。
回答by M. Bhavani
I solved it in 2 steps. I'm using MVC and I had to use this in a class file.
我分两步解决了。我正在使用 MVC,我不得不在类文件中使用它。
1) String path
1) 字符串路径
=HttpContext.Current.Server.MapPath("~/App_Data/yourxmlfilename.xml");
XDocument doc = XDocument.Load(path);
2) Change XML file properties
2) 更改 XML 文件属性
Build Action: Content
Copy to Output Directory: Copy always
构建操作:内容
复制到输出目录:始终复制