How to Format field data when the contents of a dataset are written as XML in C #

Source: Internet
Author: User

Many readers often ask a question about how to format the field data when the contents of the dataset are written as XML. The most common requirement is that you want the datetime value and the numeric data to be present in the XML in the format that you want. To achieve this, the following two approaches can be used:

Use the XmlConvert class.

Applies an XSLT transformation to the XML representation of the dataset data.

Program examples

Figure 12-23 shows the execution screen of the program Paradigm Ch12_demoform018.cs, and we find that the date and Payroll field data written from the dataset are in the format we require. Basically, this example uses the XmlConvert class to complete the formatting of a field. The related program code is written in the Click event handler for the button, as shown in the following list:

Figure 12-23

Import namespaces.
Using System.Xml;
Using System.Data.SqlClient;
Using System.IO;
private void Btnwritedatasettoxml_click (object sender, EventArgs e)
string myXMLfile = @ "C:\DataSetOutputXml.xml";
Creates a XmlTextReader object to read XML data.
using (XmlTextReader myxmlreader =
New XmlTextReader (DS. GETXML (), xmlnodetype.element, NULL)
Creates a XmlTextWriter object by using the specified file and encoding method.
using (System.Xml.XmlTextWriter Myxmlwriter =
New System.Xml.XmlTextWriter (myXMLfile, Encoding.UTF8))
myxmlwriter.formatting = formatting.indented;
Myxmlwriter.indentation = 4;
Myxmlwriter.writestartdocument ();
String elementname = "";
Resolves and displays each node.
while ( ())
Switch (myxmlreader.nodetype)
Case XmlNodeType.Element:
Myxmlwriter.writestartelement (;
ElementName =;
Case XmlNodeType.Text:
Switch (Elementname.tolower ())
Case "date of birth":
Date-time data that is required to be converted to a string is formatted with a custom format string.
Myxmlwriter.writestring (Xmlconvert.todatetime (Myxmlreader.value,
xmldatetimeserializationmode.local). ToString (
"YYYY year MMMM DD number dddd, time HH point mm minute ss seconds");
Case "Hire Date":
Case "Raise Date":
Date-time data required to be converted to a string takes a short date pattern.
Myxmlwriter.writestring (Xmlconvert.todatetime (Myxmlreader.value,
xmldatetimeserializationmode.local). ToString ("D"));
Case "Starting salary":
Case "Current Salary":
The salary value required to be converted to a string is in currency format.
Myxmlwriter.writestring (Xmlconvert.todecimal (
Myxmlreader.value). ToString ("C"));
Myxmlwriter.writestring (Myxmlreader.value);
Case XmlNodeType.EndElement:
Myxmlwriter.writeendelement ();
Reads the exported Xml file and displays its contents in the TextBox text box.
Txtxmlresult.text = File.readalltext (myXMLfile);
catch (Exception ex)
MessageBox.Show (ex. message);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.