Change all values of given tag in XML file using C# 2005

Saxitalis
Saxitalis used Ask the Experts™
on
Hello Experts,

I have an XML file like this:

<NewDataSet>
- <Table>
  <A>100</A>
  <B>200</B>
  <C>300</C>
  <D>1900-01-01T00:00:00-09:00</D>
  </Table>
- <Table>
  <A>100</A>
  <B>200</B>
  <C>300</C>
  <D>1900-01-01T00:00:00-09:00</D>
  </Table>
- <Table>
  <A>100</A>
  <B>200</B>
  <C>300</C>
  <D>1900-01-01T00:00:00-09:00</D>
  </Table>
  </NewDataSet>

I would like to change all the instances of   <D>1900-01-01T00:00:00-09:00</D>  in the XML file to <D>2010-01-01T00:00:00-09:00</D> (Replace the 1900 with 2010)

Does anyone know how to do this using C# 2005?

Thanks!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
XmlDocument doc = new XmlDocument();
    doc.Load("yourfile.xml");

    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;
    nodeList=root.SelectNodes("//D"); //this will search regardless of changes in your path

then you go a replace each node inner text and save the document.
Senior Software Engineer
Top Expert 2009
Commented:
Here is the complete code:
    XmlDocument doc = new XmlDocument();
    doc.Load("yourfile.xml");

    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;
    nodeList=root.SelectNodes("//D");
    foreach (XmlNode item in nodeList)
            {
                item.InnerText = "2010-01-01T00:00:00-09:00";
            }
    doc.Save("yourfile.xml");
Hi there,

The following code will do it, it will replace the 1900 with 2010, I've also included how to change it to the current datetime in the format you're after.

XmlDocument document = new XmlDocument();
            document.Load(fileName);
            XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
  
            XmlNodeList list = document.SelectNodes("//NewDataSet/Table/D");
            XmlNode singleNode;
            for (int i = 0; i < list.Count; i++)
            {
                singleNode = list[i];
                string data = singleNode.InnerText.ToString();
                if (data.Contains("1900"))
                {
                    singleNode.InnerText = data.Replace("1900", "2010");
                    // Or use this for a current date
                    // singleNode.InnerText = DateTime.Now.ToString("s");
                    document.Save(fileName);
                }
            }

Open in new window

Commented:
The following can also be a solution for you

            string text = "";
            using (StreamReader sr = new StreamReader("c:\\file.xml"))
            {
                text = sr.ReadToEnd();
                sr.Close();
            }
            text = text.Replace(" <D>1900-01-01T00:00:00-09:00</D>", "<D>2010-01-01T00:00:00-09:00</D>");

            using (StreamWriter sw = new StreamWriter("c:\\file.xml"))
            {
                sw.WriteLine(text);
                sw.Flush();
                sw.Close();
            }

Author

Commented:
Worked fine - Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial