1

I found this great tutorial for loading XML using XLINQ (LINQ to XML).

http://www.codearsenal.net/2012/07/c-sharp-load-xml-using-xlinq.html

It helped me a lot and I got the job done with it.

The only change I made was where he had this line:

from e in XDocument.Load(@"..\..\Employees.xml").Root.Elements("employee")

I write it like this:

from el in XDocument.Load("XML_Files/Employees.xml").Root.Elements("employee")

I had to change the path like this to access a local xml file found right inside my Visual Studio project.

But now I need to save the data back to the file in my project solution. Again, my xml file is located inside my C# project. It's not on the desktop or anything, it is a file added to the project solution.

I can't seem to find any good resources for how to do this task. Does anyone know a good tutorial, or code, a reference to start?

I am inserting a list of objects into the xml files. The objects have basic data type properties, except for one of the object properties, which is a List of doubles.

Can anyone advise a good tutorial or link? Or even a generic code sample?

I'd like to keep this function as basic as possible.

Please help.

------------------ UPDATE ------------------

I actually got this kind of working now. The below code does what I need EXCEPT that it won't write the data to my local file in the Visual Studio project. It will gladly write the data to a test file I created on my desktop, however.

Does anyone know why this is??

//create the serialiser to create the xml
XmlSerializer serialiser = new XmlSerializer(typeof(List<Student>));

// Create the TextWriter for the serialiser to use
TextWriter Filestream = new StreamWriter(@"C:\\Users\\MyName\\Desktop\\output.xml");

//write to the file
serialiser.Serialize(Filestream, employees);

// Close the file
Filestream.Close();

-------- UPDATE ---------

Okay, figured it out.

This code works:

public void WriteXML()
{
    //create the serialiser to create the xml
    XmlSerializer serialiser = new XmlSerializer(typeof(List<Student>));

    // Create the TextWriter for the serialiser to use
    TextWriter Filestream = new StreamWriter(@"XML_Files\Employees.xml");

    //write to the file
    serialiser.Serialize(Filestream, employees);

    // Close the file
    Filestream.Close();
}

The data is inserted to the xml file, but it does not show in Visual Studio. But when I checked here:

C:\Users\Me\Desktop\MyProject\MyProject\bin\Debug\XML_Files

The file is overwritten.

Also, when I reload the data from the application again, the new entries come up.

1
  • did you check the projects output directory/path? @Zolt
    – Rezoan
    Oct 3, 2013 at 9:14

1 Answer 1

-1

The problem is in line:

TextWriter Filestream = new StreamWriter(@"C:\\Users\\MyName\\Desktop\\output.xml");

Change it to one of following:

TextWriter Filestream = new StreamWriter("C:\\Users\\MyName\\Desktop\\output.xml");
TextWriter Filestream = new StreamWriter(@"C:\Users\MyName\Desktop\output.xml");

Simply remove the "@", OR use single slashes:

1
  • @Jakub Szułakiewicz, thanks, but with your suggestion I am still writing to the test file on my desktop and not to my file in my Visual Studio project. The data needs to go here to end up in my xml file: XML_Files/Employees.xml.
    – Zolt
    Oct 3, 2013 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.