Пример #1
0
        internal protected XmlElement CreateXmlForSave(XmlDocument xmlDoc, string subRootName)
        {
            XmlElement node;

            XmlElement   rootNode = xmlDoc.CreateElement(subRootName);
            XmlAttribute guid     = xmlDoc.CreateAttribute("GUID");

            guid.Value = Guid;
            rootNode.SetAttributeNode(guid);

            Dictionary <String, PropertyInfo> propSaveLoad = new Dictionary <String, PropertyInfo>();

            foreach (PropertyInfo prop in (this.GetType()).GetProperties())
            {
                foreach (object attribute in prop.GetCustomAttributes(true))
                {
                    if (attribute is SaveLoadAttribute)
                    {
                        if (string.IsNullOrEmpty(attribute.ToString()))
                        {
                            propSaveLoad.Add(prop.Name, prop);
                        }
                        else
                        {
                            propSaveLoad.Add(attribute.ToString(), prop);
                        }
                    }
                }
            }

            foreach (string nodeName in propSaveLoad.Keys)
            {
                PropertyInfo thisProp = propSaveLoad[nodeName];
                if (thisProp != null)
                {
                    object value = thisProp.GetValue(this, null);
                    if (SerializeData.Serialize(thisProp.PropertyType, value, out string valueText))
                    {
                        node = xmlDoc.CreateElement(nodeName);
                        node.AppendChild(xmlDoc.CreateTextNode(valueText));
                        rootNode.AppendChild(node);
                    }
                    else
                    {
                        if (valueText != null)
                        {
                            throw new Exception(valueText);
                        }
                        else
                        {
                            throw new Exception("in '" + thisProp.Name + "' ,Can not determine type ");
                        }
                    }
                }
            }

            InternalCreateXmlForSave(rootNode);

            return(rootNode);
        }