This question has two parts: Firstly, i need to be able to add text to an the contents of an XML element in a document when a user clicks on the 'Add Note' button in my program. I can do this no problem. However, if the user wants to add more notes later, the original notes are wiped out due to the fact that i call the 'setElementValue' method. Is there an alternative so the notes will be added to the existing text, not replace the existing text?
public static void addNote(ListViewItem file, string note)
{
int i = 0;
while (i < 20)
{
try
{
XDocument doc = XDocument.Load(Form1.CSDBpath + Form1.projectName + "\\Data.xml");
string name = file.Text;
var el = (from item in doc.Descendants("dataModule")
where item.Descendants("DMC").First().Value == name
select item).First();
if (el != null)
{
el.SetElementValue("notes", note);
}
doc.Save(Form1.CSDBpath + Form1.projectName + "\\Data.xml");
return;
}
catch
{
i++;
}
}
MessageBox.Show("Please try again in a moment, system busy (3)", "DAWS");
}
Second part of the question: As you can see in my code, i need to access and write to an xml data file which stores various pieces of information for lots of files (including notes as per above question above). This xml data file is shared by several users, and occasionally their systems are trying to access the file at the same time, which obviously causes problems. I have worked around this by looping for a set number of times - to make the program repeatedely try to open and write to the xml file. Am i going about this in the accepted fashion, or is there a better way?