1/4/12

Code Sample - Deserialize XML into an existing object


Until now, in this blog you couldn’t find any code, but I decided to put some of my own code samples together with my other posts. The first one is about how to deserialize XML into an existing and already initialized object. It's not completely generic solution and will not work for all kind of objects, but it works for my case and many others and if you want you can extend it with your own implementation for some special cases. Here it is:


        private static void DeserializeIntoExistingObject(string xmlData, object initializedObject)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlData);
            XmlNode firstNode = xmlDoc.FirstChild;
            if (firstNode != null)
            {
                foreach (XmlNode node in firstNode.ChildNodes)
                {
                    PropertyInfo property = initializedObject.GetType().GetProperty(node.Name);
                    if (property != null && node.FirstChild != null && node.FirstChild.InnerText != null)
                    {
                        object valueObject = null;
                        if (property.PropertyType == typeof(string))
                        {
                            valueObject = node.FirstChild.InnerText;
                        }
                        else
                        {
                            Type propertyType = property.PropertyType;
                            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                Type propertyTypeBase = propertyType.GetGenericArguments()[0];
                                propertyType = propertyTypeBase;
                            }
                            if (propertyType.IsEnum)
                            {
                                if (Enum.IsDefined(propertyType, node.FirstChild.InnerText))
                                {
                                    valueObject = Enum.Parse(propertyType, node.FirstChild.InnerText);
                                }
                            }
                            else
                            {
                                Type propertyRef = propertyType.MakeByRefType();
                                var method = propertyType.GetMethod("TryParse", new[] { typeof(string), propertyRef });
                                if (method != null)
                                {
                                    object[] oArgs = new object[] { node.FirstChild.InnerText, null };
                                    bool canConvert = (bool)method.Invoke(null, oArgs);
                                    if (canConvert && oArgs[1] != null)
                                    {
                                        valueObject = oArgs[1];
                                    }
                                }
                            }
                        }
                        if (valueObject != null)
                        {
                            property.SetValue(initializedObject, valueObject, null);
                        }
                    }
                }
            }
        }



4 comments:

  1. This site is interesting as well as informative. Enjoyed browsing through the site. Keep up the good work. Greetings...
    Preventative Maintenance Software

    ReplyDelete
  2. Nice Blog. This blog is very interesting and you are provide best information for users. Thanks for your wonderful sharing. Software Development

    ReplyDelete
  3. I have read this post. collection of post is a nice one ..that am doing website designing company chennai india and website development company chennai india. That I will inform about your post to my friends and all the best for your future posts..

    ReplyDelete
  4. In fact programming is nothing however it’s a logic, if you get control on it afterward you are the professional else nothing.

    BEST Podiatry Websites Design and Marketing Solutions for Doctors in America.

    ReplyDelete