I am trying to add (insert) a new <Period></Period>
element into the following XML;
<?xml version="1.0" encoding="utf-8"?>
<Scheduler>
<Module guid="64A3EB4C-7F34-47F3-8894-933CB0048D87">
<RetrieveDays>1</RetrieveDays>
<Schedule>
<Period>1</Period>
<Period>33</Period>
<Period>49</Period>
<Period>73</Period>
</Schedule>
</Module>
</Scheduler>
So for example, I pass the value 96, the new XML would look like;
<?xml version="1.0" encoding="utf-8"?>
<Scheduler>
<Module guid="64A3EB4C-7F34-47F3-8894-933CB0048D87">
<RetrieveDays>1</RetrieveDays>
<Schedule>
<Period>1</Period>
<Period>33</Period>
<Period>49</Period>
<Period>73</Period>
<Period>96</Period>
</Schedule>
</Module>
</Scheduler>
Using the following code;
// period is the new value
XDocument xmlDoc = new XDocument();
xmlDoc = XDocument.Load(String.Format(@"{0}\{1}", settingsDir, settingsFilename));
XElement periodNodes = xmlDoc.Root.Descendants("Module").Where(i => (String)i.Attribute("guid") == moduleGuId).First().Element("Schedule");
if (periodNodes.Descendants("Period").Where(x => x.Value == period.ToString()).Count() == 0)
periodNodes.Add(new XElement("Period", period.ToString()));
xmlDoc.Save(String.Format(@"{0}\{1}", settingsDir, settingsFilename));
But unfortunately no new <Period></Period>
element gets created. I have checked that the XML is valid, which it is. I tried renaming the element, but to no change.
I cannot find a solution, what am I missing?
Update
Well, this is embarrassing: I restarted the computer and VS and now it works - go figure. Nonetheless, thank you all for such quick responses and suggestions.