views:

174

answers:

5

What is the advantage of adding XML files to a visual studio 2008 project (windows form app project for example).

Once added to the project, how could I refer to this XML to use it in a class in the same project? In this case, I would be sending it as a query to a web service.

A: 

If your app needs to load the XML it can be copied to the output directory. Also simplifies use of Setup/Deployment projects...

Arnshea
+2  A: 

I guess the advantage of having your XML reside there in your project (or solution even) is that you can maintain it in VS with nice formatting and even intelli-sense, but then using something like XML Spy or whatever can give you that too.

To refer to it in a class you'll need to ensure you have access to it, and that it resides in a reliable place.

In the past I've used post build events to move the latest copy of the file to where I need it. As Arnshea writes here is another answer, "to the output directory". You can use the "Copy to Output directory" property on the XML file itself to achieve this. Then your classes can use the XML file, knowing it will reside in a reliable place.

You'll need to make sure it's accessible though especially if you're writing back to it. Make sure it doesn't end up "Read Only" - as Source Control system could do to you. Storing these files in a folder under Program Files could also be problematic especially on Vista, where user privileges are (should be) restricted.

Stuart Helwig
A: 

Another major advantage would be (assuming it's in place--and it should be!) is that you can apply revision control to the XML file.

Adam Robinson
A: 

I guess that you won't be sending the same XML file to the WebService over and over again.
You will want to modify its content every time for that you have XML Serialization.
If all of the above apply then you don't need the XML file, you just need the class that generates the file at runtime. The XML is just the transport, today its XML and tomorrow it might be some other format (JSON).

Shay Erlichmen
+3  A: 

If you want to use the XML in some form, you could mark it as a "embedded resource" in the properties window, and then access it from your code like so:

Assembly a = Assembly.GetExecutingAssembly();
if(a != null)
{
  Stream s = a.GetManifestResourceStream(typeof(yourType), "YourXmlName.xml");

  if (s != null)
  {
    String xmlContents = new StreamReader(s).ReadToEnd();
  }
}

Once you're done, you have the XML file's contents in "xmlContents", if all goes well.

Marc

marc_s