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.