YAXLib.YAXSerializer.SerializeToXDocument C# (CSharp) Method

SerializeToXDocument() public method

Serializes the specified object and returns an instance of XDocument containing the result.
public SerializeToXDocument ( object obj ) : XDocument
obj object The object to serialize.
return XDocument
        public XDocument SerializeToXDocument(object obj)
        {
            return SerializeXDocument(obj);
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// One of the base methods that perform the whole job of serialization.
        /// </summary>
        /// <param name="obj">The object to be serialized</param>
        /// <returns>an instance of <c>XElement</c> which contains the result of 
        /// serialization of the specified object</returns>
        private XElement SerializeBase(object obj)
        {
            if (!m_type.IsInstanceOfType(obj))
            {
                throw new YAXObjectTypeMismatch(m_type, obj.GetType());
            }

            // to serialize stand-alone collection or dictionary objects
            if (m_udtWrapper.IsTreatedAsDictionary)
            {
                var elemResult = MakeDictionaryElement(null, m_udtWrapper.Alias, obj,
                    m_udtWrapper.DictionaryAttributeInstance, m_udtWrapper.CollectionAttributeInstance);
                if (m_udtWrapper.PreservesWhitespace)
                    XMLUtils.AddPreserveSpaceAttribute(elemResult);
                return elemResult;
            }
            else if (m_udtWrapper.IsTreatedAsCollection)
            {
                var elemResult = MakeCollectionElement(null, m_udtWrapper.Alias, obj, null, null);
                if (m_udtWrapper.PreservesWhitespace)
                    XMLUtils.AddPreserveSpaceAttribute(elemResult);
                return elemResult;
            }
            else if(ReflectionUtils.IsBasicType(m_udtWrapper.UnderlyingType))
            {
                bool dummyAlreadyAdded;
                var elemResult = MakeBaseElement(null, m_udtWrapper.Alias, obj, out dummyAlreadyAdded);
                if (m_udtWrapper.PreservesWhitespace)
                    XMLUtils.AddPreserveSpaceAttribute(elemResult);
                return elemResult;
            }
            else if(!m_udtWrapper.UnderlyingType.EqualsOrIsNullableOf(obj.GetType()))
            {
                // this block of code runs if the serializer is instantiated with a
                // another base value such as System.Object but is provided with an
                // object of its child

                var ser = new YAXSerializer(obj.GetType(), m_exceptionPolicy,
                    m_defaultExceptionType, m_serializationOption);
                ser.SetNamespaceToOverrideEmptyNamespace(TypeNamespace);

                //ser.SetBaseElement(insertionLocation);
                var xdoc = ser.SerializeToXDocument(obj);
                var elem = xdoc.Root;

                ImportNamespaces(ser);
                m_parsingErrors.AddRange(ser.ParsingErrors);
                elem.Name = m_udtWrapper.Alias;

                elem.AddAttributeNamespaceSafe(m_yaxLibNamespaceUri + m_trueTypeAttrName, obj.GetType().FullName);
                RegisterYaxLibNamespace();

                AddNamespacesToElement(elem);

                return elem;
            }
            else
            {
                return SerializeBase(obj, m_udtWrapper.Alias);
            }
        }
All Usage Examples Of YAXLib.YAXSerializer::SerializeToXDocument