?
Solved

Referencing a file that has been added to your project

Posted on 2005-04-01
5
Medium Priority
?
247 Views
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?
0
Comment
Question by:DarcyAdams
[X]
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
5 Comments
 
LVL 8

Accepted Solution

by:
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.
   B.ReadXml(myXmlReader);

Regards -A.R
0
 

Author Comment

by:DarcyAdams
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?
0
 
LVL 8

Expert Comment

by:AaronReams
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.

Cheers,
Aaron
0
 
LVL 8

Expert Comment

by:AaronReams
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.
0
 

Author Comment

by:DarcyAdams
ID: 13683604
Works perfectly now.  Thanks!
0

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