简体   繁体   中英

Replace one Xml tag value with another tag value in C#

I have been trying to do this in C# to write a class for xml files in my folder to replace NULL value for MyXmlElement12 with Value from MyXmlElement as below +datetimestamp:

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

Could someone please help? I have been able to get the value from first element and add a time stamp as below. But how do I update the second xml tag with this value replacestring I have below?

 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("/", "");
 }
XDocument doc = XDocument.Load(Path);
doc.Element("MyXmlType")
   .Element("MyXmlElement12")
   .Value += DateTime.Now.ToString();
doc.Save(Path);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM