コード例 #1
0
        public static void SerializeToXmlNode(XmlNode Node, object Object, bool UseNodeParam)
        {
            try
            {
                XmlDocument xmlDoc = Node.OwnerDocument;

                XmlNode objNode = null;
                if (!UseNodeParam)
                {
                    Type objType = Object.GetType();

                    XmlAttribute attrAssembly = xmlDoc.CreateAttribute("Assembly");
                    XmlAttribute attrClass    = xmlDoc.CreateAttribute("Class");
                    if (objType.IsGenericType)
                    {
                        string[] typeNameParts = objType.Name.Split('`');
                        objNode = xmlDoc.CreateElement(typeNameParts[0]);
                    }
                    else
                    {
                        objNode = xmlDoc.CreateElement(objType.Name);
                    }

                    attrAssembly.Value = objType.Assembly.GetName().Name;
                    attrClass.Value    = objType.FullName;

                    objNode.Attributes.Append(attrAssembly);
                    objNode.Attributes.Append(attrClass);

                    Node.AppendChild(objNode);
                }
                else
                {
                    objNode = Node;
                    objNode.RemoveAll();

                    Type objType = Object.GetType();

                    XmlAttribute attrAssembly = xmlDoc.CreateAttribute("Assembly");
                    XmlAttribute attrClass    = xmlDoc.CreateAttribute("Class");
                    if (objType.IsGenericType)
                    {
                        string[] typeNameParts = objType.Name.Split('`');
                        objNode = xmlDoc.CreateElement(typeNameParts[0]);
                    }
                    else
                    {
                        objNode = xmlDoc.CreateElement(objType.Name);
                    }

                    attrAssembly.Value = objType.Assembly.GetName().Name;
                    attrClass.Value    = objType.FullName;

                    objNode.Attributes.Append(attrAssembly);
                    objNode.Attributes.Append(attrClass);
                }

                if (Object.GetType().IsPrimitive || Object is String || Object is Enum)
                {
                    XmlAttribute attr = xmlDoc.CreateAttribute("Value");
                    objNode.Attributes.Append(attr);

                    HandlerBase typeHandler = HandlerBase.GetTypeHandler(Object.GetType());
                    attr.Value = typeHandler.ConvertToString(Object);
                }
                else
                {
                    PropertyInfo[] props = Object.GetType().GetProperties();

                    foreach (PropertyInfo prop in props)
                    {
                        if (prop.GetCustomAttributes(typeof(NonSerializedAttribute), true).Length == 0)
                        {
                            object propValue = prop.GetValue(Object, null);
                            if (propValue != null)
                            {
                                Type propValueType = propValue.GetType();
                                if (propValueType.IsPrimitive || propValue is String || propValue is Enum || propValue is DateTime)
                                {
                                    XmlAttribute attr = xmlDoc.CreateAttribute(prop.Name);
                                    if (attr != null)
                                    {
                                        HandlerBase typeHandler = HandlerBase.GetTypeHandler(propValueType);
                                        attr.Value = typeHandler.ConvertToString(propValue);

                                        objNode.Attributes.Append(attr);
                                    }
                                }
                                else
                                {
                                    XmlNode objElement = xmlDoc.CreateNode(XmlNodeType.Element, prop.Name, string.Empty);

                                    //XmlNode objElement = null;
                                    XmlAttribute attrAssembly = xmlDoc.CreateAttribute("Assembly");
                                    XmlAttribute attrClass    = xmlDoc.CreateAttribute("Class");
                                    //if (prop.PropertyType.IsGenericType)
                                    //{
                                    //    string[] typeNameParts = prop.PropertyType.Name.Split('`');
                                    //    objElement = xmlDoc.CreateElement(typeNameParts[0]);
                                    //}
                                    //else
                                    //    objElement = xmlDoc.CreateElement(prop.PropertyType.Name);

                                    attrAssembly.Value = prop.PropertyType.Assembly.GetName().Name;
                                    attrClass.Value    = prop.PropertyType.FullName;

                                    objElement.Attributes.Append(attrAssembly);
                                    objElement.Attributes.Append(attrClass);

                                    objNode.AppendChild(objElement);

                                    if (propValue is ICollection)
                                    {
                                        saveCollection(propValue, objElement);
                                    }
                                    else
                                    {
                                        SerializeToXmlNode(objElement, propValue, true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception occurred in XmlSerializer SerializeToXmlNode", ex);
            }
        }