用c#中的另一个标记值替换一个Xml标记值

本文关键字:一个 Xml 替换 另一个 | 更新日期: 2023-09-27 18:17:09

我一直试图在c#中这样做,在我的文件夹中为xml文件编写一个类,以替换MyXmlElement12的NULL值与MyXmlElement的值,如下所示+datetimestamp:

<MyXmlType>
   <MyXmlElement>Value</MyXmlElement>
  <MyXmlElement12></MyXmlElement12>
</MyXmlType>

有人能帮帮我吗?我已经能够从第一个元素中获得值,并添加如下所示的时间戳。但是,我如何更新这个值replacestring我下面的第二个xml标记?

 public Form1()
 {
    InitializeComponent();
    XmlDocument doc = new XmlDocument();
    doc.Load("C:''Users''1''1.xml");
    XmlNode node = doc.DocumentElement.SelectSingleNode("//MyXmlElement");
    string text = node.InnerText;
    string t = text + DateTime.Now.ToString();
    replacestring= t.Replace("/", "");
 }

用c#中的另一个标记值替换一个Xml标记值

XDocument doc = XDocument.Load(Path);
doc.Element("MyXmlType")
   .Element("MyXmlElement12")
   .Value += DateTime.Now.ToString();
doc.Save(Path);