Skip to content

Commit

Permalink
Add overload SerializeToXml with object type
Browse files Browse the repository at this point in the history
  • Loading branch information
k.selivanov committed Dec 17, 2019
1 parent e449205 commit 07836b8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/XmlSerializerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public static class XmlSerializerExtensions
public static byte[] SerializeToXml<T>(this T @object)
{
var serializer = new XmlSerializer(typeof(T));
return SerializeToXml(@object, serializer);
}

public static byte[] SerializeToXml(object @object)
{
var serializer = new XmlSerializer(@object.GetType());
return SerializeToXml(@object, serializer);
}

private static byte[] SerializeToXml<T>(T @object, XmlSerializer serializer)
{
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms, Encoding.UTF8))
Expand All @@ -24,7 +35,8 @@ public static byte[] SerializeToXml<T>(this T @object)
namespaces.Add("", ns);
}

serializer.Serialize(sw, @object, namespaces ?? new XmlSerializerNamespaces(new[] {new XmlQualifiedName(string.Empty)}));
serializer.Serialize(sw, @object,
namespaces ?? new XmlSerializerNamespaces(new[] {new XmlQualifiedName(string.Empty)}));
}

return ms.ToArray();
Expand Down

0 comments on commit 07836b8

Please sign in to comment.