#region 返回DataSet
[WebMethod]
public DataSet GetAllDepTra_DataSet()
{
DataTable dt = commonService.GetAllDepTra();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
#endregion
#region 返回Xml
[WebMethod]
public XmlElement GetAllDepTra_XElement()
{
DataTable dt = commonService.GetAllDepTra();
#region 方法一
//XElement result = new XElement("Result");
//XElement search = new XElement("Accounts");
//if (dt.Rows.Count > 0)
//{
// search.Add(
// from d in dt.AsEnumerable()
// select new XElement(
// "Account"
// , new XElement("name", d.Field<string>("名称"))
// ));
// search.Add(
// new XElement("count", dt.Rows.Count)
// );
//}
//result.Add(search);
//XmlDocument doc = new XmlDocument();
//doc.LoadXml(result.ToString(SaveOptions.DisableFormatting));
//return doc.DocumentElement;
#endregion
#region 方法二 直接将DataTable转化为xml
dt.TableName = "Test";
XmlSerializer xmlSerial = new XmlSerializer(typeof(DataTable));
StringWriter sw = new StringWriter();
xmlSerial.Serialize(sw, dt); // 序列化table
string ss = sw.ToString();
XmlDocument dom = new XmlDocument();
dom.LoadXml(ss);
XmlElement root = dom.DocumentElement;
return root;
#endregion
}
#endregion
#region 返回Json
[WebMethod]
public string GetAllDepTra_Json()
{
DataTable dt = commonService.GetAllDepTra();
StringBuilder jsonString = new StringBuilder();
jsonString.Append("[");
DataRowCollection drc = dt.Rows;
for (int i = 0; i < drc.Count; i++)
{
jsonString.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
string strKey = dt.Columns[j].ColumnName;
string strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataType;
jsonString.Append("\"" + strKey + "\":");
strValue = StringFormat(strValue, type);
if (j < dt.Columns.Count - 1)
{
jsonString.Append(strValue + ",");
}
else
{
jsonString.Append(strValue);
}
}
jsonString.Append("},");
}
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
return jsonString.ToString();
}
/// <summary>
/// 过滤特殊字符
/// </summary>
/// <param name="s">字符串</param>
/// <returns>json字符串</returns>
private static string String2Json(String s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s.ToCharArray()[i];
switch (c)
{
case '\"':
sb.Append("\\\""); break;
case '\\':
sb.Append("\\\\"); break;
case '/':
sb.Append("\\/"); break;
case '\b':
sb.Append("\\b"); break;
case '\f':
sb.Append("\\f"); break;
case '\n':
sb.Append("\\n"); break;
case '\r':
sb.Append("\\r"); break;
case '\t':
sb.Append("\\t"); break;
default:
sb.Append(c); break;
}
}
return sb.ToString();
}
/// <summary>
/// 格式化字符型、日期型、布尔型
/// </summary>
/// <param name="str"></param>
/// <param name="type"></param>
/// <returns></returns>
private static string StringFormat(string str, Type type)
{
if (type == typeof(string))
{
str = String2Json(str);
str = "\"" + str + "\"";
}
else if (type == typeof(DateTime))
{
str = "\"" + str + "\"";
}
else if (type == typeof(bool))
{
str = str.ToLower();
}
else if (type != typeof(string) && string.IsNullOrEmpty(str))
{
str = "\"" + str + "\"";
}
return str;
}
#endregion
<pre name="code" class="csharp">个人还是比较倾向于Json的写法,在调用该方法时也方便使用返回结果。
Json返回结果如下:
{
[{
"名称": "tesrt",
"负责人ID": b9d6f057-ca4e-e411-a8d9-b82a72d5a332,
"培训摘要": "asdf",
"创建时间": "2015/3/16 3:23:57"
}
]
}
</pre><pre code_snippet_id="661220" snippet_file_name="blog_20150507_7_8019957" name="code" class="csharp">