public static string Serializar(object obj)
{
System.Xml.XmlDocument doc = new XmlDocument();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.IO.MemoryStream stream = new System.IO.MemoryStream();
try
{
serializer.Serialize(stream, obj);
stream.Position = 0;
doc.Load(stream);
if (doc.FirstChild.NodeType == XmlNodeType.XmlDeclaration){
XmlDeclaration xmlDeclaration = (XmlDeclaration)doc.FirstChild;
xmlDeclaration.Encoding = "ISO-8859-1";
//xmlDeclaration.Encoding = "utf-8";
}
return doc.InnerXml;
}
catch
{
throw;
}
finally
{
stream.Close();
stream.Dispose();
}
}
O código abaixo mostra como serializar um texto em XML para ser enviado informações para um webservice (por exemplo).
- 0 Users Found This Useful
Related Articles
Powered by WHMCompleteSolution