C # how to format field data when dataset content is written as XML

Source: Internet
Author: User

To achieve this purpose, you can adopt the following two methods:
◆ Use the xmlconvert class.
◆ Use An XSLT conversion set to XML Representation of dataset data.
Program Example
This example uses the xmlconvert class to format fields. CopyCode The Code is as follows: // import the namespace.
Using system. xml;
Using system. Data. sqlclient;
Using system. IO;
Private void btnwritedatasettoxml_click (Object sender, eventargs E)
{
String myxmlfile = @ "C: \ datasetoutputxml. xml ";
Try
{
// Create an xmltextreader object to read XML data.
Using (xmltextreader myxmlreader =
New xmltextreader (Ds. getxml (), xmlnodetype. element, null ))
{
// Create an xmltextwriter object 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 = "";
// Parse and display 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 ":
// The date and time data to be converted to a string must be formatted in a custom format string.
Myxmlwriter. writestring (xmlconvert. todatetime (myxmlreader. value,
Xmldatetimeserializationmode. Local). tostring (
"Dddd, Mmmm DD, yyyy, HH: mm: SS seconds "));
Break;
Case "employment date ":
Case "salary increase date ":
// The date and time data to be converted to a string must adopt the short date mode.
Myxmlwriter. writestring (xmlconvert. todatetime (myxmlreader. value,
Xmldatetimeserializationmode. Local). tostring ("D "));
Break;
Case "starting salary ":
Case "current salary ":
// The salary value to be converted to a string must be in the currency format.
Myxmlwriter. writestring (xmlconvert. todecimal (
Myxmlreader. Value). tostring ("C "));
Break;
Default:
Myxmlwriter. writestring (myxmlreader. value );
Break;
}
Break;
Case xmlnodetype. endelement:
Myxmlwriter. writeendelement ();
Break;
}
}
}
}
// Read the output XML file and display its content in the textbox text box.
Txtxmlresult. Text = file. readalltext (myxmlfile );
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}

Note: In the switch, the value after each case is the column name. Please modify it as needed.

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.