[英]Is it possible to create an empty C# solution in Visual Studio 2015
[英]C# Visual Studio 2015 load xml inside solution
我正在使用 Visual Studio 2015。我的解決方案Xml/file.xml
有一個 XML 文件
我寫:
XElement root = XElement.Load(@"Xml\file.xml");
我的file.xml
屬性是:
Build Action = Content
Copy to Output Directory = Copy Always
在調試模式下它可以工作,但是當我發布解決方案時,它會在以下位置搜索文件:
c:\windows\system32\inetsrv\Xml\file.xml
解決辦法是什么?
謝謝
如果文件大小不是很大,我個人通常會做這樣的事情作為嵌入式資源,以便將 XML 包含在您的編譯代碼中,並且您不需要加載外部文件......
設置屬性:
Build Action = EmbeddedResource
然后使用如下代碼:
// adapt as needed
string yourResourceName = "YourAssembly.Xml.File.Xml";
// load the embedded resource
Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(yourResourceName);
// if resource is found
if (resourceStream != null) {
tXElement root = XElement.Load(resourceStream);
// do whatever you want to do ....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.