示例#1
0
        public static XObject SerializeSingleValue(Type type, string xmlField, object value, bool onlyIdForObjects, bool useAttribute, bool selfOnlySerialization, bool parentSelfOnlySerialization)
        {
            if (value == null)
            {
                return(null);
            }

            XObject returnVal = null;

            if (type == typeof(Boolean))
            {
                if ((Boolean)value)
                {
                    returnVal = BusinessObjectHelper.CreateXObject(xmlField, "1", useAttribute);
                }
                else
                {
                    returnVal = BusinessObjectHelper.CreateXObject(xmlField, "0", useAttribute);
                }
            }
            else if (type == typeof(String) || type == typeof(Int32) || type == typeof(Int32?))
            {
                returnVal = BusinessObjectHelper.CreateXObject(xmlField, value.ToString(), useAttribute);
            }
            else if (type == typeof(Decimal) || type == typeof(Decimal?))
            {
                returnVal = BusinessObjectHelper.CreateXObject(xmlField, ((Decimal)value).ToString(CultureInfo.InvariantCulture), useAttribute);
            }
            else if (type == typeof(Guid) || type == typeof(Guid?))
            {
                returnVal = BusinessObjectHelper.CreateXObject(xmlField, ((Guid)value).ToUpperString(), useAttribute);
            }
            else if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                returnVal = BusinessObjectHelper.CreateXObject(xmlField, ((DateTime)value).ToIsoString(), useAttribute);
            }
            else if (type == typeof(XElement))
            {
                XElement xValue = (XElement)value;
                if ((xValue.FirstNode is XElement || xValue.HasAttributes || xValue.Name.LocalName != "value") && !useAttribute)
                {
                    returnVal = BusinessObjectHelper.CreateXObject(xmlField, value, useAttribute);
                }
                else if (!useAttribute)
                {
                    returnVal = BusinessObjectHelper.CreateXObject(xmlField, xValue.Value, useAttribute);
                }
                else
                {
                    returnVal = BusinessObjectHelper.CreateXObject(xmlField, xValue.ToString(SaveOptions.DisableFormatting), useAttribute);
                }
            }
            else if (typeof(IBusinessObject).IsAssignableFrom(type))
            {
                if (!parentSelfOnlySerialization || !selfOnlySerialization)
                {
                    IBusinessObject obj = (IBusinessObject)value;

                    if (onlyIdForObjects)
                    {
                        returnVal = BusinessObjectHelper.CreateXObject(xmlField, obj.Id.Value.ToUpperString(), useAttribute);
                    }
                    else
                    {
                        returnVal = BusinessObjectHelper.CreateXObject(xmlField, obj.Serialize(selfOnlySerialization), useAttribute);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else if (type.IsEnum)
            {
                returnVal = BusinessObjectHelper.CreateXObject(xmlField, (int)value, useAttribute);
            }
            else if (typeof(ISerializableBusinessObjectContainer).IsAssignableFrom(type))
            {
                if (!parentSelfOnlySerialization && !selfOnlySerialization)
                {
                    XElement xel = ((ISerializableBusinessObjectContainer)value).Serialize();
                    xel.Name  = xmlField;
                    returnVal = xel;
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                throw new InvalidOperationException("Unknown type to serialize");
            }

            return(returnVal);
        }