C# 在某个位置插入xml节点

C# 在某个位置插入xml节点,c#,xml,C#,Xml,我在尝试在特定位置插入xml节点时遇到了无数问题。 当我需要在之前添加节点时,我的当前代码会在之前添加节点。谢谢你看 我有以下xml文件结构: <?xml version="1.0" encoding="UTF-8"?> <project> <configuration> <general> <verbose>True</verbose> </general> <ar

我在尝试在特定位置插入xml节点时遇到了无数问题。 当我需要在
之前添加节点时,我的当前代码会在
之前添加节点。谢谢你看

我有以下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>

选择
database
节点作为
refElem
,然后使用
AppendChild
方法将
对象
节点追加到此节点:

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

这能回答你的问题吗
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);