3

Currently I am dealing with this is the type of XML: XML FILE

With reference to the XML file, I want to check for a node, if the node is not found, I have to append the node to the file. I have tried the following code:

 private void button12_Click(object sender, EventArgs e)
    {
     //   XmlNodeList func_name_value = doc.GetElementsByTagName("FUNCTION-NAME-VALUE");
        XmlNodeList list_def_ref = doc.GetElementsByTagName("DEFINITION-REF");
        foreach (XmlNode nodeDef in list_def_ref)
        {
            if (nodeDef.InnerText == "/AUTOSAR/Com/ComConfig/ComSignal")
                {
                    if (nodeDef.ParentNode.HasChildNodes)
                    {
                        XmlNodeList list = nodeDef.ParentNode.ChildNodes;
                        foreach (XmlNode node in list)
                        {
                            if (node.Name == "PARAMETER-VALUES")
                            {
                                XmlNodeList param_list = node.ChildNodes;
                                foreach (XmlNode paramNode in param_list)
                                {
                                    if (paramNode.Name == "FUNCTION-NAME-VALUE")
                                    {
                                        XmlNodeList func_child_list = paramNode.ChildNodes;
                                        foreach (XmlNode funChild in func_child_list)
                                        {
                                            if (funChild.Name == "DEFINITION-REF")
                                            {
                                                string tout = "/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification";
                                                string comnotify = "/AUTOSAR/Com/ComConfig/ComSignal/ComNotification";
                                                string invalid = "/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification";
                                                if (funChild.InnerText != tout)
                                                {
                                                    if (funChild.InnerText != comnotify)
                                                    {
                                                        //ADD ComInvalidNotification tags
                                                        XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
                                                        paramNode.AppendChild(newNode);
                                                        XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
                                                        XmlAttribute attr = doc.CreateAttribute("DEST");
                                                        attr.Value = "FUNCTION-NAME-DEF";
                                                        defRefNode.Attributes.SetNamedItem(attr);
                                                        newNode.AppendChild(defRefNode);
                                                        XmlNode val = doc.CreateElement("VALUE");
                                                        val.InnerText = "ComInvalidNotification";//ComInvalidNotification + shortName ;
                                                        newNode.AppendChild(val);
                                                    }
                                                    else
                                                    {
                                                        //ADD ComNotification tags
                                                        XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
                                                        paramNode.AppendChild(newNode);
                                                        XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
                                                        XmlAttribute attr = doc.CreateAttribute("DEST");
                                                        attr.Value = "FUNCTION-NAME-DEF";
                                                        defRefNode.Attributes.SetNamedItem(attr);
                                                        newNode.AppendChild(defRefNode);
                                                        XmlNode val = doc.CreateElement("VALUE");
                                                        val.InnerText = "ComNotification Node";//ComNotification + shortName;
                                                        newNode.AppendChild(val);
                                                    }
                                                }
                                                else
                                                {
                                                    //ADD ComTimeOutNotification tags
                                                    XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
                                                    paramNode.AppendChild(newNode);
                                                    XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
                                                    XmlAttribute attr = doc.CreateAttribute("DEST");
                                                    attr.Value = "FUNCTION-NAME-DEF";
                                                    defRefNode.Attributes.SetNamedItem(attr);
                                                    newNode.AppendChild(defRefNode);
                                                    XmlNode val = doc.CreateElement("VALUE");
                                                    val.InnerText = "ComTimeoutNotification node";//ComInvalidNotification + shortName;
                                                    newNode.AppendChild(val);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        doc.Save(openFileDialog1.FileName);

The Error I am getting is: the element list has changed. the enumeration operation failed to continue.

After the 1st execution of the foreach loop, I am getting this error, how should I overcome this error?

sre
  • 67
  • 9
  • 1
    This error says _code is trying to modify collection while enumerate through collection_, which is not allowed. try using `for` instead of `foreach`. – Hari Prasad Apr 20 '16 at 10:19
  • @Hari Prasad Do you mean to say, I have to change all **foreach** loops to **for** for all the iterations that in the code ? I have tried changing the 1st **foreach** loop to **for** previously, but the error remains the same! – sre Apr 20 '16 at 10:22
  • Yes, at least the places where you see these error(s). – Hari Prasad Apr 20 '16 at 10:24

1 Answers1

2

You have two problems here:

  1. You are modifying your XmlNodeList collections while iterating through them. It is standard practice by Microsoft to throw an exception when a collection is modified during iteration, see for instance the documentation for IEnumerator.MoveNext:

    Exceptions

    InvalidOperationException: The collection was modified after the enumerator was created.

    To avoid this exception, you can use a for loop and index through the XmlNodeList manually, or snapshot to a static List<T> and iterate through that.

  2. You are searching for XML nodes named <DEFINITION-REF> and <FUNCTION-NAME-VALUE>, but you are also creating nodes with this name. This means that nodes you create early in the iteration might be found later in the iteration, causing recursive creation of even more nodes. I reckon you do not want this. If my reckoning is correct, you should snapshot all nodes meeting your search criteria, then iterate through the snapshots, like so:

    private static void AddMissingNodes(XmlDocument doc)
    {
        var query = from nodeDef in doc.GetElementsByTagName("DEFINITION-REF").Cast<XmlNode>()
                    where nodeDef.InnerText == "/AUTOSAR/Com/ComConfig/ComSignal"
                    from nodeDefSibling in nodeDef.ParentNode.ChildNodes.Cast<XmlNode>()
                    where nodeDefSibling.Name == "PARAMETER-VALUES"
                    from paramNode in nodeDefSibling.ChildNodes.Cast<XmlNode>()
                    where paramNode.Name == "FUNCTION-NAME-VALUE"
                    select new
                    {
                        paramNode = paramNode,
                        func_child_list = (from funChild in paramNode.ChildNodes.Cast<XmlNode>()
                                          where funChild.Name == "DEFINITION-REF"
                                           select funChild).ToList() // Snapshot func_child_list by calling ToList()
                    };
    
        foreach (var paramNodeAndFuncChildren in query.ToList()) // Snapshot everything by calling ToList()
            foreach (var funChild in paramNodeAndFuncChildren.func_child_list)
                AddMissingNodes(doc, paramNodeAndFuncChildren.paramNode, funChild);
    
    }
    
    private static void AddMissingNodes(XmlDocument doc, XmlNode paramNode, XmlNode funChild)
    {
        string tout = "/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification";
        string comnotify = "/AUTOSAR/Com/ComConfig/ComSignal/ComNotification";
        string invalid = "/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification";
        if (funChild.InnerText != tout)
        {
            if (funChild.InnerText != comnotify)
            {
                //ADD ComInvalidNotification tags
                XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
                paramNode.AppendChild(newNode);
                XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
                XmlAttribute attr = doc.CreateAttribute("DEST");
                attr.Value = "FUNCTION-NAME-DEF";
                defRefNode.Attributes.SetNamedItem(attr);
                newNode.AppendChild(defRefNode);
                XmlNode val = doc.CreateElement("VALUE");
                val.InnerText = "ComInvalidNotification";//ComInvalidNotification + shortName ;
                newNode.AppendChild(val);
            }
            else
            {
                //ADD ComNotification tags
                XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
                paramNode.AppendChild(newNode);
                XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
                XmlAttribute attr = doc.CreateAttribute("DEST");
                attr.Value = "FUNCTION-NAME-DEF";
                defRefNode.Attributes.SetNamedItem(attr);
                newNode.AppendChild(defRefNode);
                XmlNode val = doc.CreateElement("VALUE");
                val.InnerText = "ComNotification Node";//ComNotification + shortName;
                newNode.AppendChild(val);
            }
        }
        else
        {
            //ADD ComTimeOutNotification tags
            XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
            paramNode.AppendChild(newNode);
            XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
            XmlAttribute attr = doc.CreateAttribute("DEST");
            attr.Value = "FUNCTION-NAME-DEF";
            defRefNode.Attributes.SetNamedItem(attr);
            newNode.AppendChild(defRefNode);
            XmlNode val = doc.CreateElement("VALUE");
            val.InnerText = "ComTimeoutNotification node";//ComInvalidNotification + shortName;
            newNode.AppendChild(val);
        }
    }
    

    Then, if I count the number of XML nodes before and after:

        var fileName = @"D:\Temp\Question36740480\autosar_ecucvalues_Fx4_L.xml";
        var newFileName = @"D:\Temp\Question36740480\autosar_ecucvalues_Fx4_L_NEW.xml";
    
        var doc = new XmlDocument();
        doc.Load(fileName);
    
        int countBefore = doc.SelectNodes("descendant::*").Count;
    
        AddMissingNodes(doc);
    
        int countAfter = doc.SelectNodes("descendant::*").Count;
    
        Debug.WriteLine(string.Format("Added {0} nodes", countAfter - countBefore));
    
        doc.Save(newFileName);
    
        Debug.WriteLine("Wrote: " + newFileName);
    

    I see that 342 nodes were added to the XmlDocument.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • I tried changing this in my code, and called the function **AddMissingNodes(doc);** in the button12 , though there are no errors this time, i don't see any changes in my XML file – sre Apr 21 '16 at 03:16
  • @ShreedharHegde - did you remember to save the file back? Using the XML file you provided I see that 342 nodes were added to the `XmlDocument`. – dbc Apr 21 '16 at 04:29
  • I tried the tradtional method using **for** loop, I am now able to add the respective node at the respective place, How should I set the namespace for the newly created nodes? I haven't yet declared any namespace in my code, but my XML already has a namespace. I want to set the existing namspace of the XML to the newly created node. Could you please help me on this? Right now this is how the new node is added **** I want that to be visible in my XML as **** but it should have the default XML namespace – sre Apr 21 '16 at 05:51
  • @ShreedharHegde - You could try [`XmlDocument.CreateElement (String, String, String)`](https://msdn.microsoft.com/en-us/library/c22k3d47%28v=vs.110%29.aspx). However, the preferred format for questions on stackoverflow is [one question per question](https://meta.stackexchange.com/questions/100380/one-question-per-page-on-http-stackoverflow-com-questions), so if you need additional, unrelated help, you may wish to ask a new question. – dbc Apr 21 '16 at 06:10
  • Thanks a lot for your help :) – sre Apr 21 '16 at 06:29