System.Xml.Serialization.XmlCustomFormatter.FromDefaultValue C# (CSharp) Method

FromDefaultValue() static private method

static private FromDefaultValue ( object value, string formatter ) : string
value object
formatter string
return string
        internal static string FromDefaultValue(object value, string formatter)
        {
            if (value == null) return null;
            Type type = value.GetType();
            if (type == typeof(DateTime))
            {
                if (formatter == "DateTime")
                {
                    return FromDateTime((DateTime)value);
                }
                if (formatter == "Date")
                {
                    return FromDate((DateTime)value);
                }
                if (formatter == "Time")
                {
                    return FromTime((DateTime)value);
                }
            }
            else if (type == typeof(string))
            {
                if (formatter == "XmlName")
                {
                    return FromXmlName((string)value);
                }
                if (formatter == "XmlNCName")
                {
                    return FromXmlNCName((string)value);
                }
                if (formatter == "XmlNmToken")
                {
                    return FromXmlNmToken((string)value);
                }
                if (formatter == "XmlNmTokens")
                {
                    return FromXmlNmTokens((string)value);
                }
            }
            throw new Exception(SR.Format(SR.XmlUnsupportedDefaultType, type.FullName));
        }

Usage Example

Example #1
0
        static internal string ExportDefaultValue(TypeMapping mapping, object value)
        {
            #if DEBUG
            // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
            if (!(mapping is PrimitiveMapping))
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
            }
            else if (mapping.IsList)
            {
                throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
            }
            #endif


            if (mapping is EnumMapping)
            {
                EnumMapping em = (EnumMapping)mapping;

                #if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (value.GetType() != typeof(string))
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), value.GetType().FullName)));
                }
                #endif

                // check the validity of the value
                ConstantMapping[] c = em.Constants;
                if (em.IsFlags)
                {
                    string[]  names  = new string[c.Length];
                    long[]    ids    = new long[c.Length];
                    Hashtable values = new Hashtable();
                    for (int i = 0; i < c.Length; i++)
                    {
                        names[i] = c[i].XmlName;
                        ids[i]   = 1 << i;
                        values.Add(c[i].Name, ids[i]);
                    }
                    long val = XmlCustomFormatter.ToEnum((string)value, values, em.TypeName, false);
                    return(val != 0 ? XmlCustomFormatter.FromEnum(val, names, ids) : null);
                }
                else
                {
                    for (int i = 0; i < c.Length; i++)
                    {
                        if (c[i].Name == (string)value)
                        {
                            return(c[i].XmlName);
                        }
                    }
                    return(null); // unknown value
                }
            }

            PrimitiveMapping pm = (PrimitiveMapping)mapping;

            if (!pm.TypeDesc.HasCustomFormatter)
            {
                #if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (pm.TypeDesc.Type == null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping for " + pm.TypeDesc.Name + " missing type property"));
                }
                #endif

                if (pm.TypeDesc.FormatterName == "String")
                {
                    return((string)value);
                }

                Type formatter = typeof(XmlConvert);
                System.Reflection.MethodInfo format = formatter.GetMethod("ToString", new Type[] { pm.TypeDesc.Type });
                if (format != null)
                {
                    return((string)format.Invoke(formatter, new Object[] { value }));
                }
            }
            else
            {
                string defaultValue = XmlCustomFormatter.FromDefaultValue(value, pm.TypeDesc.FormatterName);
                if (defaultValue == null)
                {
                    throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
                }
                return(defaultValue);
            }
            throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
        }