日期:2014-05-18 浏览次数:20814 次
public StringBuilder SerializeDS(System.Data.DataTable pDT) { try { StringBuilder mSB = new StringBuilder(); string mSplitChar = System.Text.Encoding.Default.GetString(new byte[] { 0x1E }); mSB.Append("<DSRoot>"); mSB.Append("<DSHead>"); if (pDT.Columns.Count > 0) { mSB.Append(pDT.Columns[0].ColumnName + ":" + pDT.Columns[0].DataType.Name); for (int i = 1; i < pDT.Columns.Count; i++) { mSB.Append(mSplitChar + pDT.Columns[i].ColumnName + ":" + pDT.Columns[i].DataType.Name); } } mSB.Append("</DSHead>"); mSB.Append("<DSBody>"); for (int i = 0; i < pDT.Rows.Count; i++) { mSB.Append("<DSRow>"); if (pDT.Columns.Count > 0) { mSB.Append(GetSerializeString(pDT.Columns[0].DataType, pDT.Rows[i][0])); for (int j = 1; j < pDT.Columns.Count; j++) { mSB.Append(mSplitChar + GetSerializeString(pDT.Columns[j].DataType, pDT.Rows[i][j])); } } mSB.Append("</DSRow>"); } mSB.Append("</DSBody>"); mSB.Append("</DSRoot>"); return mSB; } catch (NullReferenceException NullEx) { throw NullEx; } catch (Exception Ex) { throw Ex; } } private string GetSerializeString(Type pType, object pValue) { try { if (pType == typeof(System.String)) { if (pValue != System.DBNull.Value) { return pValue.ToString(); } else { return ""; } } else if (pType == typeof(System.DateTime)) { try { if (pValue.ToString().Trim() != "") { return ((System.DateTime)pValue).ToString("yyyy-MM-dd HH:mm:ss"); } else { return "1900-01-01"; } } catch { return "1900-01-01"; } } else if ((pType == typeof(System.Int32)) || (pType == typeof(System.Int16)) || (pType == typeof(System.UInt32)) || (pType == typeof(System.UInt16)) || (pType == typeof(System.Decimal)) || (pType == typeof(System.Double)) ) { try { if (pValue.ToString().Trim() != "") { return pValue.ToString(); } else { return "0"; } } catch { return "0"; } } else if (pType == typeof(System.Boolean)) { try {