• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Referencing a file that has been added to your project

I know this is an easy questions, but I don't know the answer.  I have added an xml file to my VS project.  How do I reference the file that I added to the project?  

My code says:

DataSet B = new DataSet();
B.ReadXml( What goes here?)

I added a file to my project called test.xml

Any ideas?
0
DarcyAdams
Asked:
DarcyAdams
  • 3
  • 2
1 Solution
 
AaronReamsCommented:
Hi DarcyAdams,

You need to read the XML from a stream.

   DataSet B = new DataSet();
     
   // Create new FileStream to read schema with.
   System.IO.FileStream fsReadXml = new System.IO.FileStream("test.xml", System.IO.FileMode.Open);

   // Create an XmlTextReader to read the file.
   System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml);

   // Read the XML document into the DataSet.
   B.ReadXml(myXmlReader);

Regards -A.R
0
 
DarcyAdamsAuthor Commented:
Thank you, that adds another dimension that I didn't know.  When I run this code it returns:

Additional information: Could not find file "C:\WindowsApplication1\bin\Debug\2Returned.xml".

The file is located in  "C:\WindowsApplication1\test.xml"  because that's where it defaulted to when I added it to my project.  Is there a different way of referencing that besides doing the path everytime?
0
 
AaronReamsCommented:
Not that I know of right off hand.   I would just add a relative reference to the file as @"..\..\test.xml".  Keep in mind when you actually install this application on your clients machine, you'll know where the file is installed.  

That way you can reference it by the default "test.xml" (by putting it in the same directory as the application), or using a relative path to the installed executable, "xml_files\test.xml", or absolute path by retreiving the application path programatically or from the registry "C:\Program Files\My Application\test.xml".

Hope this helps.

Cheers,
Aaron
0
 
AaronReamsCommented:
One other thing you can always do is set the WorkingDirectory in the Project Properties to your source directory where the xml file is located.  Then you can just reference it by calling "test.xml".

Project Properties > Configuration Properties > Debugging > Working Directory.

Good luck -A.R.
0
 
DarcyAdamsAuthor Commented:
Works perfectly now.  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now