У меня есть этот код, который хорошо работает, но я не могу понять, как на самом деле сохранить обновление списка XML.descendents = JobNames в XML-документе.
public void ProcessJobNames(String JobName = null, Boolean rRemove = false)
{
System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Load(_JobPath + @"\" + _ConfigFile);
var JobNames = xml.Descendants("setting").Where(xe => xe.Attribute("key").Value.Contains("JobType")).ToList();
System.Xml.Linq.XElement copyXE = null;
int JobCount = 0;
foreach (System.Xml.Linq.XElement strJobName in JobNames)
{
//strJobNames.Add(strJobName.LastAttribute.Value.ToString());
JobCount = JobCount + 1;
strJobName.FirstAttribute.Value = "JobType" + (JobCount).ToString();
copyXE = strJobName; // Make a copy of the Job
// Check if the JobName is in the Job Processor to be Removed
if ((JobName != null) && (rRemove == true) && (strJobName.LastAttribute.Value.ToString().Contains(JobName)))
{
// Remove the Job from the Configuration File
strJobName.Remove();
JobCount = JobCount - 1;
}
// Check if the JobName is in the Job Processor to be Added
if ((JobName != null) && (rRemove == false) && (strJobName.LastAttribute.Value.ToString().Contains(JobName)))
{
// Add the Job from the Configuration File
strJobName.Remove();
JobCount = JobCount - 1;
}
}
// Add the new Job
if ((JobName != null) && (rRemove == false)) {
File.Copy(_RulesFolder + cmboRules.Text, _JobPath + cmboRules.Text);
System.Xml.Linq.XElement newJob = new System.Xml.Linq.XElement(copyXE);
newJob.FirstAttribute.Value = "JobType" + (JobCount + 1).ToString();
newJob.LastAttribute.Value = _JobPrefix + cmboRules.Text;
copyXE.Parent.Add(newJob)
}
// Save the XML Configuration File
xml.Save(_JobPath + _ConfigFile);
}
Я думаю, что 'после «JobNames.Add (newJob)» что-то отсутствует, чтобы обновить потомков в документе XML.