0

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?

3 Answers 3

1

How about:

el.SetElementValue("notes", el.value + " " + note);

As for the second part, it doesn't sound like a very scaleable solution you have. What about writing a small service (e.g. WCF) and have that access a single set of files?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That was easy! :) As for the second part, i'll have to look into that as i've no idea how to set that up. Thanks for the pointer though.
0
if (string.IsNullOrWhiteSpace(el.Value))
    el.Value = note;
else
    el.Value += Environment.NewLine + note;

For the second issue you might want to save the file to a new filename (data.xml.tmp) and after that call File.Move to rename it. This way the file will not be accessible for much shorter duration.

Also review System.IO.Path.Combine() - you should not concatenate paths as strings.

Comments

0

I'm not sure how your XML code looks, it would be nice with an example, but you could try adding a sibling using the XmlNode's ParentNode and AppendChild.

Also, you should not use First() if your sequence might be empty. Use FirstOrDefault instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.