コード例 #1
0
        protected override XElement Convert(DataRow row, string name)
        {
            XElement element = new XElement(name);

            foreach (DataColumn column in row.Table.Columns)
            {
                XElement xColumn = new XElement(column.ColumnName);

                object obj = row[column];
                if (obj is DBNull)
                {
                    xColumn.Value = string.Empty;
                    xColumn.SetAttributeValue(XSINamespace + "nil", "true");
                }
                else if (obj is bool)
                {
                    xColumn.Value = ((bool)obj) ? "true" : "false";
                }
                else if (obj is DateTime)
                {
                    xColumn.Value = DateFormatter.Format((DateTime)obj);
                }
                else if (obj is byte[] bytes)
                {
                    xColumn.Value = System.Convert.ToBase64String(bytes);
                }
                else
                {
                    xColumn.Value = obj.ToString();
                }

                element.Add(xColumn);
            }
            return(element);
        }
コード例 #2
0
ファイル: ToJsonConverter.cs プロジェクト: shantiw/Entitybase
        protected void Append(StringBuilder sb, DataRow row)
        {
            foreach (DataColumn column in row.Table.Columns)
            {
                sb.Append("\"");
                sb.Append(column.ColumnName);
                sb.Append("\":");

                object obj = row[column];
                if (obj is DBNull)
                {
                    sb.Append("null,");
                }
                else if (obj is bool)
                {
                    if ((bool)obj)
                    {
                        sb.Append("true,");
                    }
                    else
                    {
                        sb.Append("false,");
                    }
                }
                else if (obj is DateTime date)
                {
                    sb.Append("\"");
                    sb.Append(DateFormatter.Format(date));
                    sb.Append("\",");
                }
                else if (obj is string value)
                {
                    value = value.Replace("\t", string.Empty).Replace("\v", string.Empty);
                    value = value.Replace("\r\n", "\\r\\n");
                    sb.Append("\"");
                    sb.Append(value);
                    sb.Append("\",");
                }
                else if (IsNumeric(obj))
                {
                    sb.Append(obj.ToString());
                    sb.Append(",");
                }
                else if (obj is byte[] bytes)
                {
                    sb.Append("\"");
                    sb.Append(System.Convert.ToBase64String(bytes));
                    sb.Append("\",");
                }
                else
                {
                    sb.Append("\"");
                    sb.Append(obj.ToString());
                    sb.Append("\",");
                }
            }
        }