您好我有app.exe.config文件中创建/更新字段的功能
public static void UpdateConfig(string FieldName, string FieldValue, ConfigSelector SectionName = ConfigSelector.AppSettings) { switch (SectionName) { case ConfigSelector.Execption: { // MessageBox.Show("gg"); var xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); if (xmlDoc.SelectSingleNode("configuration/Execption") != null) { if (xmlDoc.SelectSingleNode("configuration/Execption/List") != null) { // create new node <add key="Region" value="Canterbury" /> var nodeRegion = xmlDoc.CreateElement("add"); nodeRegion.SetAttribute("key", FieldName); nodeRegion.SetAttribute("value", FieldValue); xmlDoc.SelectSingleNode("configuration/Execption/List").AppendChild(nodeRegion); } else { var List = xmlDoc.CreateElement("List"); xmlDoc.SelectSingleNode("configuration/Execption").AppendChild(List); UpdateConfig(FieldName, FieldValue, SectionName); } } else { var List = xmlDoc.CreateElement("Execption"); xmlDoc.SelectSingleNode("configuration").AppendChild(List); UpdateConfig(FieldName, FieldValue, SectionName); } xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); ConfigurationManager.RefreshSection("Execption/List"); break; } } }函数首先检查是否存在xpath配置/执行,如果不存在则创建此路径并再次调用该函数,如果不创建路径并再次调用函数,则第二次检查配置/执行/列表路径是否存在,第三次添加必填字段是fieldname和fieldvalue,
但我得到System.StackOverflowException行:
if (xmlDoc.SelectSingleNode("configuration/Execption") != null)我错过了什么?
Hello I have function which creates/updates fields in app.exe.config file
public static void UpdateConfig(string FieldName, string FieldValue, ConfigSelector SectionName = ConfigSelector.AppSettings) { switch (SectionName) { case ConfigSelector.Execption: { // MessageBox.Show("gg"); var xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); if (xmlDoc.SelectSingleNode("configuration/Execption") != null) { if (xmlDoc.SelectSingleNode("configuration/Execption/List") != null) { // create new node <add key="Region" value="Canterbury" /> var nodeRegion = xmlDoc.CreateElement("add"); nodeRegion.SetAttribute("key", FieldName); nodeRegion.SetAttribute("value", FieldValue); xmlDoc.SelectSingleNode("configuration/Execption/List").AppendChild(nodeRegion); } else { var List = xmlDoc.CreateElement("List"); xmlDoc.SelectSingleNode("configuration/Execption").AppendChild(List); UpdateConfig(FieldName, FieldValue, SectionName); } } else { var List = xmlDoc.CreateElement("Execption"); xmlDoc.SelectSingleNode("configuration").AppendChild(List); UpdateConfig(FieldName, FieldValue, SectionName); } xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); ConfigurationManager.RefreshSection("Execption/List"); break; } } }Function works first Check if xpath configuration/Execption exist, if not exist it creates this path and recalls function again, second time check if configuration/Execption/List path exist if not creates path and recalls function again, and third time adds required fields which is fieldname and fieldvalue,
but I getting System.StackOverflowException in line:
if (xmlDoc.SelectSingleNode("configuration/Execption") != null)Did I miss something?
最满意答案
添加新元素后不保存文档,因此当您在下一次迭代中加载文件时,新元素不存在,并且xmlDoc.SelectSingleNode("configuration/Execption") != null仍为false,所以代码在无限递归中再次创建元素,你得到StackOverflowException 。
只需在更改后保存文档即可
else { var List = xmlDoc.CreateElement("Execption"); xmlDoc.SelectSingleNode("configuration").AppendChild(List); xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); UpdateConfig(FieldName, FieldValue, SectionName); }You don't save the document after adding the new element, so when you are loading the file in the next iteration the new element isn't there, and xmlDoc.SelectSingleNode("configuration/Execption") != null is still false, so the code creates the element again in infinite recursion and you get StackOverflowException.
Just save the document after you change it
else { var List = xmlDoc.CreateElement("Execption"); xmlDoc.SelectSingleNode("configuration").AppendChild(List); xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); UpdateConfig(FieldName, FieldValue, SectionName); }更多推荐
发布评论