1

I'm having endless issues trying to insert xml node at a specific location. My current code adds a node before </project> when I need it to add it to before </database>. Thanks for taking a look!

I have the following xml file structure:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <configuration>
    <general>
      <verbose>True</verbose>
    </general>
    <architecture>
      <site reference="Demo Site Reference" type="WTP" id="0001" />
      <rtu dnp="77" ip="10.10.10.77" type="Schneider Electric SCADAPack 442" />
      <radio ip="10.10.10.76" />
      <hmi ip="10.10.10.75" />
    </architecture>
  </configuration>
  <database>
    <object id="0" name="object 0" description="Entry Description 0" date="22.06.2018 00:00:00" archestra="Export">
      <attributes>
        <attribute id="0" name="Attribute 0" description="Attribute 0 Description" note="Attribute 0 Note" />
        <attribute id="1" name="Attribute 1" description="Attribute 1 Description" note="Attribute 1 Note" />
      </attributes>
    </object>
    <object id="1" name="object 1" description="Entry Description 1" date="22.06.2018 00:00:00" archestra="Export">
      <attributes>
        <attribute id="0" name="Attribute 0" description="Attribute 0 Description" note="Attribute 0 Note" />
        <attribute id="1" name="Attribute 1" description="Attribute 1 Description" note="Attribute 1 Note" />
        <attribute id="2" name="Attribute 2" description="Attribute 2 Description" note="Attribute 2 Note" />
        <attribute id="3" name="Attribute 3" description="Attribute 3 Description" note="Attribute 3 Note" />
      </attributes>
    </object>
    <object id="2" name="object 2" description="Entry Description 2" date="22.06.2018 00:00:00" archestra="Export">
      <attributes>
        <attribute id="0" name="Attribute 0" description="Attribute 0 Description" note="Attribute 0 Note" />
        <attribute id="1" name="Attribute 1" description="Attribute 1 Description" note="Attribute 1 Note" />
        <attribute id="2" name="Attribute 2" description="Attribute 2 Description" note="Attribute 2 Note" />
      </attributes>
    </object>
  </database>
</project>

The following routine adds a new node at the wrong position:

    private void cmdAdd_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database.xml"));

        //** Change This **
        string query = "/project/database/object";

        //now we loop through the list
        int Maxi = 0;
        foreach (XmlNode xmlnode in doc.SelectNodes(query))
        {
            int ultimoID = Convert.ToInt32(xmlnode.Attributes["id"].Value);
            if (ultimoID > Maxi)
            {
                Maxi = ultimoID;
            }
        }
        Maxi++;

        XmlNode refElem = doc.DocumentElement.PreviousSibling.LastChild; // Last node

        XmlElement entryElement = doc.CreateElement("object");

        entryElement.SetAttribute("id", Convert.ToString(Maxi));
        entryElement.SetAttribute("date", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        entryElement.SetAttribute("name", Convert.ToString(txtName.Text));
        entryElement.SetAttribute("description", Convert.ToString(txtConfigDescription.Text));
        entryElement.SetAttribute("archestra", Convert.ToString(txtArchestra.Text));

        doc.DocumentElement.InsertBefore(entryElement, refElem); //add new node in end of file

        doc.Save(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database.xml"));

        this.Close();
    }
  • Does this answers your question [reference](https://stackoverflow.com/questions/10578983/inserting-xml-node-at-specific-position) – Gaurav Jalan Jul 03 '18 at 11:04

1 Answers1

1

Select the database node as the refElem and then use the AppendChild method to append the object node to this one:

XmlNode refElem = doc.DocumentElement.SelectSingleNode("database");

XmlElement entryElement = doc.CreateElement("object");
entryElement.SetAttribute("id", Convert.ToString(Maxi));
entryElement.SetAttribute("date", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
entryElement.SetAttribute("name", Convert.ToString(txtName.Text));
entryElement.SetAttribute("description", Convert.ToString(txtConfigDescription.Text));
entryElement.SetAttribute("archestra", Convert.ToString(txtArchestra.Text));

refElem.AppendChild(entryElement);
mm8
  • 163,881
  • 10
  • 57
  • 88