Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have xml string data and need to update the specific value in that xml return updated xml string.

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
   </book>
   <created>
   <createddate>
   <DateTime>
      2015-05-16T05:50:06.7199222-04:00
   <DateTime>
   <createddate>
   <created>
 <catalog>


I want to update <datetime> with current DateTime.Now

What I have tried:

I have tried with below code.
string xmlData="
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
   </book>
   <created>
   <createddate>
   <DateTime>
      2015-05-16T05:50:06.7199222-04:00
   <DateTime>
   <createddate>
   <created>
 <catalog>
";
;
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xmlData);
xmlDoc.SelectSingleNode("Catalog/Created/createddate/DateTime").InnerText = DateTime.Now;
xmlDoc.Save(xmlData);



but not working could you please assist on this.
Posted
Updated 28-Jan-21 2:18am
Comments
Estys 28-Jan-21 8:17am    
XML names are case-sensitive, so 'Catalog' != 'catalog'. Your examples suffer from that.

1 solution

First of all you XML is not a valid XML file.
It should look like this
XML
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
   </book>
   <created>
   <createddate>
   <DateTime>
      2015-05-16T05:50:06.7199222-04:00
   </DateTime>
   </createddate>
   </created>
 </catalog>

An open tag must have a close tag.
e.g.
XML
<created></created>

Moreover your SelectSingleNode call does not respect capitalisation.
It must rather be like this:
C#
xmlDoc.SelectSingleNode("catalog/created/createddate/DateTime").InnerText = DateTime.Now.ToString("dd.MM.yyyy");

InnerText is of type string.
Therefore you must convert your DateTime object to a string as it is shown in the example above.
 
Share this answer
 
Comments
DGKumar 28-Jan-21 8:33am    
Hi Steve
I have modified with below code
string xmlData = @"<catalog><book><author> Gambardella, Matthew <title> XML Developers Guide<created><createddate><datetime>2015";
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xmlData);
xmlDoc.SelectSingleNode("catalog/created/createddate/DateTime").InnerText = DateTime.Now.ToString("dd.MM.yyyy");
xmlDoc.Save(xmlData);

Now i am getting System.ArgumentException: 'Illegal characters in path.' exception
DGKumar 28-Jan-21 8:38am    
Is there any better approach for this ?
TheRealSteveJudge 28-Jan-21 8:55am    
Your XML is not valid.
You can store the XML I suggested in a file e.g. input.xml
and load it like this:
var xmlDoc = new XmlDocument();
xmlDoc.Load("input.xml");
Estys 28-Jan-21 9:04am    
xmlDoc.Save() expects a filename/path, not an xml-string.
TheRealSteveJudge 28-Jan-21 9:13am    
That's right. But the System.ArgumentException already comes when loading the XML.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900