Пример #1
0
        public object Xml2Obj(string xml, Type type)
        {
            if (xml == null)
            {
                return(null);
            }
            if (type.IsSubclassOf(typeof(IXmlConverter)))
            {
                IXmlConverter converter = GetDefaultValue(type) as IXmlConverter;
                converter.Xml2Object(xml);
                return(converter);
            }
            if (IsPrimitiveXmlType(type) && !IsXmlString(xml))
            {
                if (type == typeof(string))
                {
                    return(xml);
                }

                if (string.IsNullOrEmpty(xml))
                {
                    return(GetDefaultValue(type));
                }
                if (type == typeof(int))
                {
                    return(Convert.ToInt32(xml, CultureInfo.InvariantCulture));
                }
                if (type == typeof(double))
                {
                    return(Convert.ToDouble(xml, CultureInfo.InvariantCulture));
                }
                ;
                if (type == typeof(bool))
                {
                    return(Convert.ToBoolean(xml, CultureInfo.InvariantCulture));
                }
                if (type == typeof(DateTime))
                {
                    xml = string.Format(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", "dateTime", xml);
                    return(MsgTransfer.Xml2Object(xml, type, true));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(xml))
                {
                    return(GetDefaultValue(type));
                }
                return(MsgTransfer.Xml2Object(xml, type, true));
            }
            Exception <TypeConvertExceptionMessage> exception = new Exception <TypeConvertExceptionMessage>();

            exception.Reference = xml;
            exception.ErrorInfo.TargetTypeName = type.FullName;
            throw exception;
        }
Пример #2
0
        static public string Obj2Xml(object result)
        {
            if (result == null)
            {
                return(null);
            }
            IXmlConverter converter = result as IXmlConverter;

            if (converter != null)
            {
                return(converter.Object2Xml());
            }
            string rltString = MsgTransfer.Object2XmlString(result);

            if (!IsPrimitiveXmlType(result.GetType()))
            {
                return(rltString);
            }
            Match match = xmlTextRegex.Match(rltString);

            return(match.Groups[1].Value);
        }