XMLConverter.ToXml C# (CSharp) Method

ToXml() public static method

Converts an anonymous type to an XElement.
public static ToXml ( object obj, bool includetype = false ) : System.Xml.Linq.XElement
obj object
includetype bool
return System.Xml.Linq.XElement
    public static XElement ToXml(object obj, bool includetype = false)
    {
        return ToXml(obj, null, includetype);
    }

Same methods

XMLConverter::ToXml ( object obj, string element, bool includetype = false ) : System.Xml.Linq.XElement

Usage Example

Ejemplo n.º 1
0
    private static string ToXML <T>(T obj, bool includetype, bool indent = false)
    {
        if (includetype)
        {
            XElement xml = XMLConverter.ToXml(obj, null, includetype);

            if (indent)
            {
                return(xml.ToString());
            }
            else
            {
                return(xml.ToString(SaveOptions.DisableFormatting));
            }
        }
        else
        {
            System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
            XmlSerializer xs       = new XmlSerializer(typeof(T));
            StringBuilder sbuilder = new StringBuilder();
            var           xmlws    = new System.Xml.XmlWriterSettings()
            {
                OmitXmlDeclaration = true, Indent = indent
            };
            ns.Add(string.Empty, string.Empty);
            using (var writer = System.Xml.XmlWriter.Create(sbuilder, xmlws))
            {
                xs.Serialize(writer, obj, ns);
            }
            string result = sbuilder.ToString();
            ns       = null;
            xs       = null;
            sbuilder = null;
            xmlws    = null;
            return(result);
        }
    }