SoFunction
Updated on 2025-04-06

How to format field data when the content of C# DataSet is written into XML


// Import namespace.
using ;
using ;
using ;
private void btnWriteDataSetToXml_Click(object sender, EventArgs e)
{
string myXmlFile = @"C:\";
try
{
// Create an XmlTextReader object to read XML data.
using(XmlTextReader myXmlReader =
new XmlTextReader((), , null))
{
// Create an XmlTextWriter object using the specified file and encoding method.
using( myXmlWriter =
new (myXmlFile, Encoding.UTF8))
{
= ;
= 4;
();
string elementName = "";
// parse and display each node.
while(())
{
switch()
{
case :
();
elementName = ;
break;
case :
switch(())
{
case "Date of Birth":
// The date and time data converted to strings are required to be formatted using a custom format string.
((,
).ToString(
"yyyy year MMMM dd number dddd, time HH point mm minutes ss seconds"));
break;
case "hire date":
case "pay increase date":
// Requires the date and time data converted to strings to be in a short date mode.
((,
).ToString("d"));
break;
case "starting salary":
case "Current salary":
// Requires the salary value converted to strings to be in currency format.
((
).ToString("c"));
break;
default:
();
break;
}
break;
case :
();
break;
}
}
}
}
// Read the output Xml file and display its contents in the TextBox text box.
= (myXmlFile);
}
catch(Exception ex)
{
();
}
}