SelectSingleNode中的StackOverflow(StackOverflow in SelectSingleNode)

编程入门 行业动态 更新时间:2024-10-19 17:45:03
SelectSingleNode中的StackOverflow(StackOverflow in SelectSingleNode)

您好我有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); }

更多推荐

本文发布于:2023-08-07 07:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SelectSingleNode   StackOverflow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!