user8670136
user8670136

Reputation:

How to save objects from a list in XML file?

sorry for the question, but I need help. I have a program, on which you can create cars and people. You can edit or remove the cars/people or do other things.

Now I want so save the cars and people in a XML file. But I never worked with XML before, I'm a beginner.

So I want to start with the cars. I have a list with cars and with "producer, colour, license plate, ..."

so here is what I have:

    public static void SaveFileAuto(List<Car> cars)
    {
        Car car = new Car();

        XmlSerializer ser = new XmlSerializer(typeof(Car));
        StringWriter writer = new StringWriter();

        FileStream str = new FileStream(@"car.xml", FileMode.Create);
        ser.Serialize(str, cars);
    }

So, I have no idea, what to do next or what is missing or wrong.

Upvotes: 0

Views: 149

Answers (2)

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43886

At first, the Car car = new Car(); line as well as the StringWriter writer... line are obviously obsolete.

Secondly, you want to serialize a List<Car>, not simply a Car. So you have to create the XmlSerializer accordingly.

Third point: wrap the usage of the stream in a using statement, so it is cleanly closed after usage:

public static void SaveFileAuto(List<Car> cars)
{
    // create serializer for typeof(List<Car>) not typeof(Car)
    XmlSerializer ser = new XmlSerializer(typeof(List<Car>));

    using (FileStream str = new FileStream(@"car.xml", FileMode.Create))
        ser.Serialize(str, cars);
}

To load and deserialize the xml file again, you can also use the XmlSerializer:

XmlSerializer serializer = new XmlSerializer(typeof(List<Car>));

List<car> cars;
using(FileStream stream = new FileStream(@"car.xml", FileMode.Open))
    cars = (List<Car>)serializer.Deserialize(stream);

Upvotes: 4

Josh Adams
Josh Adams

Reputation: 2099

You can create the xml however you'd like. You are better off using the XmlSerializer. Referenced From Here

// Your Car Class
public class Car
{
    public string Producer{ get; set; }
    public string Colour{ get; set; }
    public int LicensePlate { get; set; }
    public int CarID { get; set; }
}

// The List<Car>
var cars= new List<Car>(){ 
    new Car() { Producer= "Ford", Colour= "Red", LicensePlate= 123},
    new Car() { Producer= "Chevy", Colour= "Green", LicensePlate= 333}       
    };

// Build the document
public static void SaveFileAuto(List<Car> cars)
{
  XDocument xdoc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
        // This is the root of the document
        new XElement("Cars", 
        from car in cars
        select
            new XElement("Car", new XAttribute("ID", car.CarID),
            new XElement("Producer",car.Producer),
            new XElement("Colour", car.Colour),
            new XElement("LicensePlate", car.LicensePlate));

   // Write the document to the file system            
   xdoc.Save("C:/Working Directory/Cars.xml");
}

Upvotes: 1

Related Questions