src/openSourceC.DotNetLibrary.Core/XmlSerialize.cs
/// <summary>
///		Serializes the specified object and returns the XML fragment as an <see cref="T:XElement"/>.
/// </summary>
/// <param name="type">The type of the object to serialize.</param>
/// <param name="obj">The <see cref="object"/> to serialize.</param>
/// <param name="supportXsiNamespace"><b>true</b> to support xsi namespace (i.e. xsi:type,
///		xsi:nil, xsi:schema, xsi:schemaLocation, and xsi:noNamespaceSchemaLocation).</param>
/// <param name="expandForReadability"><b>true</b> to expand the XML with line feeds and
///		spacing, <b>false</b> to remove all readability formatting.</param>
/// <returns>
///		An <see cref="T:XElement"/> representation of the object.
/// </returns>
Serializes the specified object and returns the XML fragment as an .
Serializes the specified object and returns the XML fragment as an .
public static XElement SerializeToXElement(Type type, object obj, bool supportXsiNamespace = false, bool expandForReadability = false)
		{
#if DEBUG
			try
			{
#endif
				using (MemoryStream stream = new MemoryStream())
				using (XmlWriter xmlWriter = XmlWriter.Create(stream, GetNewXmlWriterSettingsObject(expandForReadability)))
				{
					XmlSerializer serializer = new XmlSerializer(type);
					serializer.Serialize(xmlWriter, obj, (supportXsiNamespace ? _xmlnsNil : _xmlnsEmpty));
					xmlWriter.Flush();
					stream.Seek(0L, SeekOrigin.Begin);
					return XElement.Load(stream, (expandForReadability ? LoadOptions.PreserveWhitespace : LoadOptions.None));
				}
#if DEBUG
			}
			catch (Exception)
			{
				throw;
			}
#endif
		}
public static XElement SerializeToXElement ( Type type , object obj , bool supportXsiNamespace = false , bool expandForReadability = false ) { if DEBUG try { endif using ( MemoryStream stream = new MemoryStream ( ) ) using ( XmlWriter xmlWriter = XmlWriter . Create ( stream , GetNewXmlWriterSettingsObject ( expandForReadability ) ) ) { XmlSerializer serializer = new XmlSerializer ( type ) ; serializer . Serialize ( xmlWriter , obj , ( supportXsiNamespace ? _xmlnsNil : _xmlnsEmpty ) ) ; xmlWriter . Flush ( ) ; stream . Seek ( 0L , SeekOrigin . Begin ) ; return XElement . Load ( stream , ( expandForReadability ? LoadOptions . PreserveWhitespace : LoadOptions . None ) ) ; } if DEBUG } catch ( Exception ) { throw ; } endif }
Serializes the specified object and returns the XML fragment as an .
Serializes the specified object and returns the XML fragment as an .
// This XmlWriter ensures that the XML is formatted correctly.
An  representation of the object.
An representation of the object .
type
Type
The type of the object to serialize.
The type of the object to serialize .
obj
object
The  to serialize.
The to serialize .
supportXsiNamespace
bool
None
expandForReadability
bool
None