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.