C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Format DateTime Values in XML


Posted Date: 19 Feb 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Kamliesh NadarMember Level: Gold    
Rating: 1 out of 5Points: 10



We use the GetXml() method of the Dataset object to retrieve the data in the XML format. The problem that one faces with the XML is that the DateTime variable returned would not be in the desired format.

This article describes the process of how to format the DateTime value with minimum effort and resources.

The GetXml() method loads the Xml in the string format into the string variable. Then DateTime values in this string needs to be formatted. To achive this we load this Xml into the XmlDocument object. Then use the XmlNode object to iterate through each Datetime element and format it. Then Finally asign the newly formatted Xml string from the XmlDocument back into the strng variable.

NameSpace Used -

System.Data
System.XML
System.Text


Solution:

DataSet ds = new DataSet();
String strXML;
SqlConnection con = new SqlConnection ("User ID=sa; Password=pwd; Initial Catalog=Pubs; Data Source=servername);

SqlDataAdapter da = new SqlDataAdapter("SELECT emp_id, fname, lname, hire_date FROM Employee WHERE minit='M'", con);
da.Fill(ds);

strXML = ds.GetXml();

System.Xml.XmlDocument objDoc = new System.Xml.XmlDocument();
System.Xml.XmlNodeList objNodeLst;

objDoc.LoadXml(strXML);
// LoadXml method is used to load the XML from the string into the XmlDocument.

objNodeLst = objDoc.SelectNodes("//NewDataSet/Table/hire_date");
foreach(System.Xml.XmlNode objNode in objNodeLst)
{
//DateTime value is formatted here as per your need.
DateTime dt = DateTime.Parse(objNode.InnerText);
objNode.InnerText = dt.ToShortDateString();

}
strXML=objDoc.InnerXml;


Thus we can achive this with least effort and minimum resources.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sending email with an attachment
Previous Resource: Save The Image Into SQL Server 2000 Database
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use