Ejemplo n.º 1
0
        static XmlNode GetNodeFromObject(object value, XmlDocument dataFile)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Starting {0}", MethodBase.GetCurrentMethod().ToString());
            }
            XmlNode     node    = null;
            XmlComment  cm      = null;
            IXmlStorage storage = value as IXmlStorage;

            if (value != null)
            {
                foreach (Attribute attrib in value.GetType().GetCustomAttributes(true))
                {
                    XmlConversionRootAttribute rootAttrib = attrib as XmlConversionRootAttribute;
                    if (rootAttrib != null)
                    {
                        node = dataFile.CreateElement(rootAttrib.RootNodeName);
                    }
                    XmlCommentAttribute comment = attrib as XmlCommentAttribute;
                    if (comment != null)
                    {
                        cm = dataFile.CreateComment(comment.Comment);
                    }
                }
                if (node != null && cm != null)
                {
                    node.AppendChild(cm);
                }
                if (node != null)
                {
                    AddStorageElements(node, storage, dataFile);
                }
            }

            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Ending {0}", MethodBase.GetCurrentMethod().ToString());
            }
            return(node);
        }
Ejemplo n.º 2
0
        static void BuildXmlDocument(XmlDocument doc, string name, object value, XmlNode currentNode)
        {
            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Starting {0}", MethodBase.GetCurrentMethod().ToString());
            }



            foreach (System.Attribute attr in value.GetType().GetCustomAttributes(true))
            {
                XmlCommentAttribute nodeAttribute = attr as XmlCommentAttribute;
                if (nodeAttribute != null && !string.IsNullOrEmpty(nodeAttribute.Comment))
                {
                    XmlComment cm = doc.CreateComment(nodeAttribute.Comment);
                    currentNode.AppendChild(cm);
                }
            }

            //now need to identify value type.  If is value type, then create attribute to add to current node.
            if (value.GetType() == typeof(int) ||
                value.GetType() == typeof(decimal) ||
                value.GetType() == typeof(string) ||
                value.GetType() == typeof(double) ||
                value.GetType() == typeof(short) ||
                value.GetType() == typeof(byte) ||
                value.GetType() == typeof(long) ||
                value.GetType() == typeof(float) ||
                value.GetType() == typeof(bool))
            {
                XmlAttribute attrib = doc.CreateAttribute(name);

                if (value != null)
                {
                    string val = value.ToString();
                    if (!string.IsNullOrEmpty(val))
                    {
                        attrib.Value = value.ToString();

                        currentNode.Attributes.Append(attrib);
                    }
                }
            }
            else if (value.GetType().ImplementsIList())
            {
                IList itemList = (IList)value;
                foreach (object val in itemList)
                {
                    BuildXmlDocument(doc, name, val, currentNode);
                }
            }
            else
            {
                XmlElement newElem = doc.CreateElement(name);
                //if (ConvertObjectToXml(value, doc, newElem))
                //{
                ConvertObjectToXml(value, doc, newElem);
                currentNode.AppendChild(newElem);
                //}
            }


            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Ending {0}", MethodBase.GetCurrentMethod().ToString());
            }
        }