Method for convert DataTable to XML in C#:
public string ConvertToXML(DataTable
dt)
{
MemoryStream mstr = new MemoryStream();
dt.WriteXml(mstr, true);
mstr.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(mstr);
string
xmlString;
xmlString = sr.ReadToEnd();
return
(xmlString);
}
OR
///
///
This method is used to convert the DataTable into string XML format.
///
///
DataTable to be converted./// (string) XML form of the DataTable.
private static string
ConvertDataTableToXML(DataTable dtBuildSQL)
{
DataSet
dsBuildSQL = new DataSet();
StringBuilder sbSQL;
StringWriter swSQL;
string
XMLformat;
sbSQL = new
StringBuilder();
swSQL = new
StringWriter(sbSQL);
dsBuildSQL.Merge(dtBuildSQL, true, MissingSchemaAction.AddWithKey);
dsBuildSQL.Tables[0].TableName = "Table";
foreach (DataColumn col in
dsBuildSQL.Tables[0].Columns)
{
col.ColumnMapping = MappingType.Attribute;
}
dsBuildSQL.WriteXml(swSQL, XmlWriteMode.WriteSchema);
XMLformat = sbSQL.ToString();
return
XMLformat;
}
Good Article ..
ReplyDeletethanks..for DataTable to Xml..
ReplyDeletegood work
any syntax that can convert that string to xml element?
ReplyDeleteI mean the shortest, im working on my final year project so im trying to make my coding short