2

I created a class called Shipment which has the following static method:

public static void WriteShipment(Shipment s, string path)
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "\t";
    XmlWriter w = XmlWriter.Create(path, settings);
    w.WriteStartDocument();
    w.WriteStartElement("Shipment");
    w.WriteStartElement("id");
    w.WriteString(s.id);
    w.WriteEndElement();
    Address.WriteAddress(s.from, path);
    Address.WriteAddress(s.to, path);
    Date.WriteDate(s.receiveDate, path);
    Date.WriteDate(s.deliverDate, path);
    w.WriteStartElement("sum");
    w.WriteValue(s.sum);
    w.WriteEndElement();
    w.WriteStartElement("currency");
    w.WriteString(s.currency);
    w.WriteEndElement();
    w.WriteStartElement("paid");
    w.WriteValue(s.paid);
    w.WriteEndElement();
    w.WriteEndElement();
    w.WriteEndDocument();
    w.Close();
}

I'm trying to write a method which receives an instance of the Shipment class and creates an XML file with its details.

Some of Shipment's fields are of type Address and Date, which are other classes I created. They also have static methods which write the details of an instance, received as a parameter, into an XML file.

The WriteAddress and WriteDate methods work perfectly well, but when I try to invoke them inside the WriteShipment method, I get the following exception during run-time -

"the process cannot access file because it is used by another process"

I figured out it happens because WriteAddress and WriteDate are trying to write into the same file WriteShipment already writes into (since they all share the same path).

Is there a way to overcome this? Any other solution I've tried proved to be futile or caused other problems.

Slack Groverglow
  • 846
  • 7
  • 25
Blabla
  • 121
  • 5
  • Use try and catch and using...Check this link --> https://stackoverflow.com/questions/1346995/how-to-create-a-xmldocument-using-xmlwriter-in-net – waltmagic May 11 '18 at 03:49

2 Answers2

2

The problem you are seeing is likely because the WriteAddress and WriteDate methods are attempting to open the file that is already opened.

When you make the call to XmlWriter.Create, the file is being 'locked' such that it can only be written to using the w variable, and it remains locked until you call w.Close().

Your best option would be to pass the w variable as an argument to the WriteAddress and WriteDate methods and that to write to the file.

Also, as a suggestion, consider putting your code into a using block. That way, if any of the methods throw an exception, you won't be left with the file still locked.

Ryan S
  • 521
  • 2
  • 6
  • Thanks! I've tried to pass my XmlWriter as a parameter before, but then I had no way to indent it and all the XML code in the created file was written in a single line. Do you know how can I both pass my XmlWriter as a parameter and make it indented (which the settings variable in the beginning of the code currently does)? – Blabla May 11 '18 at 03:45
  • I second Ryan S. Use the try catch and using blocks. Also if you are now having formatting issues you might want to update your question or start a new question if your runtime exception no longer is an issue. Be sure and mark the answer that solved your problem ;) – waltmagic May 11 '18 at 04:00
  • The indenting should work automatically. The `XmlWriter` contains the settings for indenting that you provided when you created the `XmlWriter` object. Try it again, and if it still doesn't work, please post your code for the WriteAddress method. – Ryan S May 11 '18 at 04:01
0

There are many ways to fix this. Here are some suggestions.

  1. Close the XmlWriter in WriteShipment before you execute the WriteDate and WriteAddress methods. Reopen it after you're done
  2. Instead of opening and writing to the file in WriteData/Address just return a string and then write it to the file in WriteShipment. Is there any case where you need to write directly using WriteData/Address, and not as part through the WriteShipment method?
Slack Groverglow
  • 846
  • 7
  • 25