C# WebServer 生成XML类型给android
代码示例:
public class CreateXML
{
public CreateXML() {
}
/// <summary>
/// Datatable转换为xml
/// </summary>
/// <param name="table">Datatable对象</param>
/// <returns>Json字符串</returns>
public static string ToString(DataTable table, string XMLName)
{
string jsonString = "";
DataRowCollection drc = table.Rows;
for (int i = 0; i < drc.Count; i++)
{
jsonString += "<" + XMLName + " id=\""+(i+1)+"\">";
foreach (DataColumn column in table.Columns)
{
jsonString += "<" + column.ColumnName + ">";
if (column.DataType == typeof(DateTime) || column.DataType == typeof(string))
{
jsonString += drc[i][column.ColumnName] + "</" + column.ColumnName + ">";
}
else
{
jsonString += drc[i][column.ColumnName] + "</" + column.ColumnName + ">";
}
}
jsonString += "</" + XMLName + ">";
}
return jsonString;
}
/// <summary>
/// DataSet转换为Xml
/// </summary>
/// <param name="dataSet">DataSet对象</param>
/// <returns>Json字符串</returns>
public static string ToString(DataSet dataSet,string XMLName)
{
string jsonString = "<?xml version=\"1.0\" encoding=\"utf-8\"?> ";
foreach (DataTable table in dataSet.Tables)
{
jsonString += "<" + XMLName + "s>" + ToString(table, XMLName) + "</" + XMLName + "s>";
}
return jsonString;
}
}