Esempio n. 1
0
        /// <summary>
        /// Generate Xml string node from object
        /// </summary>
        /// <param name="inputValue">Input object</param>
        /// <param name="type">Object type</param>
        /// <returns>Xml string node</returns>
        public static string GenerateXmlNodeFromObject(object inputValue, Type type)
        {
            StoreType(type);

            StringBuilder xmlString = new StringBuilder();

            PropertyInfo[] fields = type.GetProperties();

            xmlString.Append(string.Format("<{0}>", type.Name));

            foreach (PropertyInfo field in fields)
            {
                if (!BaseTypeCollection.GetBaseTypes().Any(t => t == field.PropertyType))
                {
                    xmlString.Append(GenerateXmlNodeFromObject(field.GetValue(inputValue, field.GetIndexParameters()), field.PropertyType));
                }
                else
                {
                    xmlString.Append(
                        string.Format("<{0}>{1}</{0}>",
                                      field.Name,
                                      SerializerObject(field.GetValue(inputValue, field.GetIndexParameters())))
                        );
                }
            }

            xmlString.Append(string.Format("</{0}>", type.Name));

            return(xmlString.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Generate Xml string node from object
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="inputValue">Input object</param>
        /// <returns>Xml string node</returns>
        public static string GenerateXmlNodeFromObject <T>(T inputValue)
        {
            StoreType(typeof(T));

            StringBuilder xmlString = new StringBuilder();

            Type type = inputValue.GetType();

            PropertyInfo[] fields = type.GetProperties();

            xmlString.Append(string.Format("<{0}>", type.Name));

            foreach (PropertyInfo field in fields)
            {
                // If filed type is not basic type (string ,int ect...) get again
                if (!BaseTypeCollection.GetBaseTypes().Any(t => t == field.PropertyType))
                {
                    xmlString.Append(GenerateXmlNodeFromObject(field.GetValue(inputValue, field.GetIndexParameters()), field.PropertyType));
                }
                else
                {
                    xmlString.Append(
                        string.Format("<{0}>{1}</{0}>",
                                      field.Name,
                                      SerializerObject(field.GetValue(inputValue, field.GetIndexParameters())))
                        );
                }
            }

            xmlString.Append(string.Format("</{0}>", type.Name));

            return(xmlString.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Generate json string from xml node
        /// </summary>
        /// <param name="nodeString">Xml node whole information</param>
        /// <param name="type">Result object type</param>
        /// <returns>Json result string</returns>
        public static string GenerateJsonStringFromXmlMode(string nodeString, Type type)
        {
            XmlDocument document = new XmlDocument();

            document.LoadXml(nodeString);

            PropertyInfo[] fields = type.GetProperties();

            XmlNode node = document.SelectSingleNode(type.Name);

            // Get json string from xml
            StringBuilder sb = new StringBuilder();

            sb.Append("\"" + type.Name + "\":{");
            foreach (PropertyInfo field in fields)
            {
                if (!BaseTypeCollection.GetBaseTypes().Any(s => s.Equals(field.PropertyType)))
                {
                    // Check if this node is the last one in xml
                    sb.Append(
                        GenerateJsonStringFromXmlMode(
                            node.SelectSingleNode(field.Name).OuterXml,
                            field.PropertyType,
                            !node.LastChild.Name.Equals(field.Name))
                        );
                }
                else
                {
                    bool isEnum = true;

                    try
                    {
                        var m = Enum.GetValues(field.PropertyType).GetValue(
                            Enum.GetNames(field.PropertyType).ToList().
                            IndexOf(node.SelectSingleNode(field.Name).InnerText));
                        try
                        {
                            int enumValue = (int)m;
                            sb.Append(string.Format("\"{0}\":{1},", field.Name, enumValue));
                        }
                        catch { sb.Append(string.Format("\"{0}\":{1},", field.Name, m)); }
                    }
                    catch { isEnum = false; }

                    if (!isEnum)
                    {
                        sb.Append(string.Format("\"{0}\":{1},", field.Name, node.SelectSingleNode(field.Name).InnerText));
                    }
                }
            }

            if (fields.Length > 1)
            {
                sb.Remove(sb.Length - 1, 1);
            }

            sb.Append("}");

            return(sb.ToString());
        }
Esempio n. 4
0
        /// <summary>
        /// Store type for serialize
        /// </summary>
        /// <param name="type">The type that need to store</param>
        private static void StoreType(Type type)
        {
            if (KnowTypes == null)
            {
                KnowTypes = new List <Type>();
            }

            if (!KnowTypes.Any(t => t == type) && !BaseTypeCollection.GetBaseTypes().Any(s => s == type))
            {
                KnowTypes.Add(type);
            }
        }