在某个位置插入xml节点

时间:2018-07-03 10:45:21

标签: c# xml

我在尝试将xml节点插入特定位置时遇到了无尽的问题。 我当前的代码需要在</project>之前将节点添加到</database>之前。感谢您的关注!

我具有以下xml文件结构:

<?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>

以下例程在错误的位置添加了新节点:

    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();
    }

1 个答案:

答案 0 :(得分:1)

选择database节点作为refElem,然后使用AppendChild方法将object节点附加到该节点:

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);