Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);


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
C#
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";


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) 
going backward and not only XMLFile2.xml, is there a way to change that ?
Posted
Updated 1-Apr-19 5:53am
v4
Comments
[no name] 8-Aug-13 11:08am    
"If a loose resource is not found, null is returned.", http://msdn.microsoft.com/en-us/library/system.windows.application.getcontentstream.aspx
Fredrik Bornander 9-Aug-13 9:32am    
You get "not valid port" because you haven't allow pack URIs.
New up an Application object and it'll register it for you.

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"));

As long as the xml file is set as Resource.

/Fredrik
 
Share this answer
 
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?
 
Share this answer
 
Uri is probably choking on the "#". Try encoding the path.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900