Referencing a file that has been added to your project

Posted on 2005-04-01
Medium Priority
Last Modified: 2010-04-16
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?
Question by:DarcyAdams
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

AaronReams earned 2000 total points
ID: 13683074
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.

Regards -A.R

Author Comment

ID: 13683265
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?

Expert Comment

ID: 13683343
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.


Expert Comment

ID: 13683410
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.

Author Comment

ID: 13683604
Works perfectly now.  Thanks!

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question