getContentStream all在C#控制台中返回null [英] getContentStream all gives back null in a C# console

查看:85
本文介绍了getContentStream all在C#控制台中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在通过C#控制台模式进行一些测试,以便加载somme xml文件。



我有XMLFile1.xml,其中包含build作为资源,XMLFile2.xml作为内容并始终复制。



Hi,
I was going through some test via a C# console mode in order to load somme xml files.

I have XMLFile1.xml with build as resources and XMLFile2.xml with as content and always copy.

string chemin1 = "XMLFile1.xml";
Uri uri1 = new Uri(chemin1, UriKind.RelativeOrAbsolute);
StreamResourceInfo info1 = Application.GetResourceStream(uri1);






and

string chemin2 = "XMLFile2.xml";
Uri uri2 = new Uri(chemin2, UriKind.RelativeOrAbsolute);
StreamResourceInfo info2 = Application.GetContentStream(uri2);





program.cs文件与xml处于同一级别文件和第一个解决方案是好的,info1不是null但我不能让第二个工作,info2总是null。

我尝试了很多字符串例如



the program.cs file is at the same level as both xml files and the first solution is ok, info1 is not null but i can't get to make the second work out, info2 is always null.
I Have tried many string such as

chemin2 = "/Debug/XMLFile2.xml";
chemin2 = "/testC#/ConsoleApplication1/ConsoleApplication1/bin/Debug/XMLFile2.xml";
chemin2 = "file:///D:/testC#/ConsoleApplication1/ConsoleApplication1/bin/Debug/XMLFile2.xml";
chemin2 = "pack://siteoforigin:,,,/XMLFile2.xml";
chemin2 = "pack://application:,,,/XMLFile2.xml";
chemin2 = "pack://application:,,,/bin/Debug/XMLFile2.xml";
chemin2 = "/ConsoleApplication1;/XMLFile2.xml";





但我无法解决这个问题,提前感谢任何人可以为我的法语提供的任何帮助。



------------------

1:一个非常愚蠢的问题,你称之为项目的根源是.csproj在哪里?

2:我尝试了你的解决方案,我得到一个无效端口的例外

3:我真的要让getcontent版本工作并理解它

4:我觉得很奇怪,我必须使用



but i can't get it to work out, thanks in advance for any help anyone could provide sorry for my french.

------------------
1: a very dumb question, what you call the root of the project is where the .csproj is?
2: i tried with your solution and i get an exception with non valid Port
3: i'd really to make the getcontent version workd and understand it
4: i find it strange that i have to use

chemin2 = "../../XMLFile2.xml"; 
doc = XElement.Load(chemin2) 

后退而不仅仅是XMLFile2.xml,有没有办法改变它?

going backward and not only XMLFile2.xml, is there a way to change that ?

推荐答案

如果您的xml文件位于项目的根目录下,它应该可以正常运行;

If your xml file is at the root of your project, it should work just going;
var x = Application.GetResourceStream(new Uri("pack://application:,,,/XMLFile1.xml"));



只要将xml文件设置为Resource。



/ Fredrik


As long as the xml file is set as Resource.

/Fredrik


我不知道的是,我认为当一个人将文件的构建参数设置为visual studio中的内容时,它会将其添加到包中,是不是我想念?
the thing i don't get is that i thought that when one's sets the file's build parameter to content in visual studio it adds it to the pack, what is it that i miss?


Uri可能会对#感到窒息。尝试编码路径。
Uri is probably choking on the "#". Try encoding the path.


这篇关于getContentStream all在C#控制台中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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