I am getting this error sometimes when I am trying to write a message to the xml file:
500 - Internal Server Error System.IO.IOException: Sharing violation on path /var/www/isoTopix/alt/importantMsg.xml at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0 at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in ...
I have the following code that writes to the file
[WebMethod]
public void AddToXMLFile (string param)
{
XmlDocument doc = new XmlDocument ();
DateTime today = DateTime.Today;
XmlNode rootNode = doc.CreateElement ("Scandiatransplant");
XmlNode dateNode = doc.CreateElement ("Date");
XmlNode importantMsg = doc.CreateElement ("Message");
XmlAttribute msgId = doc.CreateAttribute ("id");
msgId.Value = "msgID";
dateNode.InnerText = today.ToString ();
importantMsg.InnerText = param;
importantMsg.Attributes.Append (msgId);
rootNode.AppendChild (dateNode);
rootNode.AppendChild (importantMsg);
doc.AppendChild (rootNode);
doc.Save (Global.sAppPath + "/alt/importantMsg.xml");
}
And the the code that reads from it
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public string GetMessage() {
XmlTextReader reader = new XmlTextReader (Global.sAppPath + "/alt/importantMsg.xml");
string message = null;
while (reader.Read()) {
if (reader.IsStartElement ()) {
switch (reader.Name.ToString ()) {
case "Message":
message = reader.ReadString();
break;
}
}
}
return message;
}
I have seen a solution to a similar problem, where they are using a using statement like this:
XmlWriterSettings settings = new XmlWriterSettings();
settings.CloseOutput = true;
using(XmlWriter writer = XmlWriter.Create(file.CreateText(), settings)){
//do work here
}
But I am not using a XMLWriter, so I can't do this.
I have tried doing this:
using(XmlDocument doc = new XmlDocument ())
But this does not work
Any ideas to what I can do to solve this issue?
No comments:
Post a Comment