更改 XML 文件中的元素内容

Change Element content in XML file

我在文件“test.xml”

中低于 XML
<MainDoc version="1.0" application="App2">
  <property name="AutoHiddenPanelCaptionShowMode">ShowForAllPanels</property>
  <property name="DockingOptions" isnull="true" iskey="true">
    <property name="DockPanelInTabContainerTabRegion">DockImmediately</property>
  </property>
  <property name="Panels" iskey="true" value="4">
    <property name="Item1" isnull="true" iskey="true">
      <property name="Text">ContainerABC</property>
      <property name="Options" isnull="true" iskey="true">
        <property name="AllowFloating">true</property>
      </property>
    </property>
    <property name="Item2" isnull="true" iskey="true">
      <property name="Text">ContainerXYZ</property>
    </property>
    <property name="Item3" isnull="true" iskey="true">
      <property name="Text">Container123</property>
    </property>
    <property name="Item4" isnull="true" iskey="true">
      <property name="Text">panelContainer1</property>
    </property>
  </property>
</MainDoc>

我想将上面显示“panelContainer1”的元素内容更改为“Container456”。我怎样才能做到这一点。我在下面尝试过,但不确定如何获取该内容并对其进行更改。

using System.Xml.Linq;

private void button1_Click(object sender, EventArgs e)
        {
            string xmlPath = @"C:\Downloads\test.xml";
            XDocument doc = XDocument.Load(xmlPath);
            var items = from item in doc.Descendants("property")
                        where item.Attribute("name").Value == "Item4"
                        select item;
            foreach (XElement itemElement in items)
            {
                //something here ?
            }
        }

一切看起来都很好。而你需要的是 itemElement.Value

XDocument doc = XDocument.Load(xmlPath);
var items = doc.Root.Descendants("property")
                    .Where(x => x.Attribute("name").Value == "Item4")
                    .Descendants()
                    .Where(x=> x.Attribute("name").Value == "Text");

foreach(var itemElement in items)
{
    itemElement.Value = "Container456";
}

doc.Save(xmlPath);