Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing an XML File within the Project 1

Status
Not open for further replies.

BDRichardson

Programmer
Jul 23, 2003
46
GB
Does anyone know how to reference an XML that is stored within my project?

The file is used for storing a very small 'database' and program settings. I can get it to work by hard-coding the path, or using the System.Environment.CurrentDirectory member. But I dont think this is satisfactory, because I would like to keep the file with the application, and be able to use it, wherever (in the file system) the program is installed.

Simple test:
Code:
private void Test 
{ 
     string sXmlFile = 
          System.Environment.CurrentDirectory.ToString() 
          + @"\TheXmlFile.xml"; 

     DataSet ds = new DataSet(); 
     ds.ReadXml(sXmlFile); 
}
 
Solution 1:
1. Projerct Properties->Add ->Add New Item-> choose XML file
2. Let say the default name is XMLFil1.xml.
3. You should have XMLFile1.xml as item in the solution explorer.
4. Right Click on this item in the solution explorer and in the Properties -> Build Action ->Embedded Resource
5. Somewhere in code:
Code:
System.IO.Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourAssembly.XMLFile1.xml");
DataSet ds = new DataSet(); 
ds.ReadXml(fs);
-obislavu-
 
Thanks very much for your assistance. I have been trying to do just that, but the silly thing is, I overlooked the Embedded Resource property.

Fresh input does wonders!

Cheers fella
 
can you modify this file and save it back to the assembly?

Is that possible?
 
Probably possible, but not a good idea.

1) If it were in the GAC, you likely would be denied write access, and if you did have write access, it would change the checksum on the file, and the strong name for the file wouldn't match any more (Bad Things Happen here...)

2) If it weren't in the GAC, a self-modifying executable would likely bring it to the attention of the anti-virus utility, which would probably quarantine it.

3) Might cause product support difficulties --- "Gee, it works on my machine! But yours is newer, and 230 bytes smaller!?!"

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph, I thought of that soon after I posted. I've noticed my McAfee firewall will treat my EXE's as a new program each time it is recompiled, and will ask me to allow it access to the internet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top