Today I'm going to show you some helper methods which will be help you to overcome some problems when you are dealing with XML Serialization.
/// <summary>
/// This method will convert given Object to a XML File
/// </summary>
/// <typeparam name="T">Type of the Object</typeparam>
/// <param name="item">Object which is going to store in XML File</param>
/// <param name="rootName">Put Root name if XML Root name is different from it's Original Name</param>
/// <returns>XML document with object collection Data</returns>
public static XmlDocument SerializeToXml<T>(T item, string rootName)
{
try
{
XmlSerializer serializer = null;
serializer = string.IsNullOrEmpty(rootName) ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
XmlDocument xmlDocument = new XmlDocument();
serializer.Serialize(stringWriter, item);
xmlDocument.Load(stringBuilder.ToString());
return xmlDocument;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// This method will convert given Object to a XML File
/// </summary>
/// <typeparam name="T">Type of the Object</typeparam>
/// <param name="item">Object which is going to store in XML File</param>
/// <param name="rootName">Put Root name if XML Root name is different from it's Original Name</param>
/// <returns>XML document with object collection Data</returns>
public static string SerializeToString<T>(T item, string rootName)
{
try
{
XmlSerializer serializer = string.IsNullOrEmpty(rootName) ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
serializer.Serialize(stringWriter, item);
return stringBuilder.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// This method will Serialize Object list to XML and Returns
/// XML file as string variable
/// </summary>
/// <typeparam name="T">Type of the Object</typeparam>
/// <param name="items">Object which are going to stored as XML</param>
/// <param name="rootName">Put a Root name if Root name is different from it's Original Name</param>
/// <returns>XML File as a string variable</returns>
public static string SerializeListToXml<T>(List<T> items, string rootName)
{
try
{
XmlSerializer serializer = string.IsNullOrEmpty(rootName) ? new XmlSerializer(typeof(List<T>)) : new XmlSerializer(typeof(List<T>), new XmlRootAttribute(rootName));
XmlDocument xmlDocument = new XmlDocument();
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, items);
stream.Position = 0;
xmlDocument.Load(stream);
}
return GetXmlAsString(xmlDocument);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// The serialize list to xml.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objectToEncode">The object to encode.</param>
/// <param name="encoding">The encoding.</param>
/// <param name="rootName">Name of the root.</param>
/// <returns>Serialized string</returns>
public static string SerializeToXml<T>(T objectToEncode, Encoding encoding, string rootName)
{
using (var stringWriter = new StringWriter())
{
// Encoding.GetEncoding(1252)
var settings = new XmlWriterSettings
{
Encoding = encoding,
OmitXmlDeclaration = true
};
using (var writer = XmlWriter.Create(stringWriter, settings))
{
var xmlSerializer = string.IsNullOrEmpty(rootName) ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
xmlSerializer.Serialize(writer, objectToEncode);
}
return stringWriter.ToString();
}
}
/// <summary>
/// Gets the XML as string.
/// </summary>
/// <param name="xml">The XML.</param>
/// <returns>String of Xml</returns>
private static string GetXmlAsString(XmlDocument xml)
{
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xml.WriteTo(xmlWriter);
string str = stringWriter.ToString();
return str;
}
Comments
Post a Comment