How to Format field data when the contents of the C # DataSet are written in XML _c# tutorial

Source: Internet
Author: User
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
This example uses the XmlConvert class to complete the formatting of a field.
Copy Code code as follows:

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";
Try
{
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 (Myxmlreader.read ())
{
Switch (myxmlreader.nodetype)
{
Case XmlNodeType.Element:
Myxmlwriter.writestartelement (Myxmlreader.name);
ElementName = Myxmlreader.name;
Break
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");
Break
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"));
Break
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"));
Break
Default
Myxmlwriter.writestring (Myxmlreader.value);
Break
}
Break
Case XmlNodeType.EndElement:
Myxmlwriter.writeendelement ();
Break
}
}
}
}
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);
}
}

Note: The value behind each case in the switch is the name of the column, and the reader is required to modify it.

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: info-contact@alibabacloud.com 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.