0

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("/", "");
 }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
arun thakur
  • 147
  • 1
  • 4
  • 14

1 Answers1

1
XDocument doc = XDocument.Load(Path);
doc.Element("MyXmlType")
   .Element("MyXmlElement12")
   .Value += DateTime.Now.ToString();
doc.Save(Path);
arunlalam
  • 1,838
  • 2
  • 15
  • 23
  • I am using XMLdocument above, is this same as Xdocument? Is there a way it can be done using XMLdocument? – arun thakur Jun 04 '13 at 21:57
  • XmlDocument is different than XDocument. XDocument allows you to use LINQ. I haven't personally used XmlDocument but you can see the difference here. http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – arunlalam Jun 04 '13 at 22:20
  • I got it working Thanks a lot. How can I loop through each xml file in a folder for this I am doing right now XDocument doc1 = XDocument.Load("C:\\Users\\1.xml"); – arun thakur Jun 04 '13 at 22:20
  • Use List MyFiles = Directory.GetFiles(@"C:\Dir", "*.xml").ToList(); and iterate through the list. – arunlalam Jun 04 '13 at 22:24