Exemplo n.º 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);
        }
Exemplo n.º 2
0
        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("\",");
                }
            }
        }
        internal DateFormatter Create(string dateFormatter, TimeSpan timezoneOffset, string format = null)
        {
            if (Cache == null)
            {
                Update();
            }

            XElement xFormatter = Cache[dateFormatter];
            string   type       = xFormatter.Attribute("type").Value.Split(',')[0].Trim();

            switch (type)
            {
            case "XData.Data.Objects.DotNETDateFormatter":
                return(string.IsNullOrWhiteSpace(format) ? new DotNETDateFormatter() : new DotNETDateFormatter(format));

            case "XData.Data.Objects.JsonNETFormatter":
                return(new XData.Data.Objects.JsonNETFormatter()
                {
                    TimezoneOffset = timezoneOffset
                });

            default:
                break;
            }

            XElement xFormat = xFormatter.Element("format");

            if (xFormat != null && !string.IsNullOrWhiteSpace(format))
            {
                xFormat.SetAttributeValue("value", format);
            }

            ObjectCreator objectCreator = new ObjectCreator(xFormatter);
            object        obj           = objectCreator.CreateInstance();
            DateFormatter formatter     = obj as DateFormatter;

            formatter.TimezoneOffset = timezoneOffset;
            return(formatter);
        }