Array
(
)

Erro ao Serializar Objeto(Classe) em XML CSharp

Riechelmann
   - 07 mai 2015

Boa Tarde
No Visual Studio 2008 está me retornando o Erro na exception Erro ao gerar o documento XML. e na InnerException O inicializador de tipo de 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPedidos' acionou uma exceção. porém com o mesmo código no Visual Studio 2013 ele serializa normalmente, tenho que usar o 2008, é um Projeto de Integration Services
Código para Serializar Objeto
#Código
public static string ObjectToXml<T>(T obj)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringWriter strWriter = new StringWriter();
XmlWriter xmlWriter = XmlWriter.Create(strWriter);
serializer.Serialize(xmlWriter, obj);
return RemoveAllNamespaces(strWriter.ToString());
}
//Implemented based on interface, not part of algorithm
public static string RemoveAllNamespaces(string xmlDocument)
{
XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));
return xmlDocumentWithoutNs.ToString();
}
//Core recursion function
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
if (!xmlDocument.HasElements)
{
XElement xElement = new XElement(xmlDocument.Name.LocalName);
xElement.Value = xmlDocument.Value;
foreach (XAttribute attribute in xmlDocument.Attributes())
xElement.Add(attribute);
return xElement;
}
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
}

Classe que estou utilizando para Serializar
#Código
[DataContract]
public class Invoiced
{
[DataMember]
public int number { get; set; }
[DataMember]
public int line { get; set; }
[DataMember]
public string issueDate { get; set; }
[DataMember]
public string key { get; set; }
}

[DataContract]
public class Pf
{
[DataMember]
public string cpf { get; set; }
[DataMember]
public string name { get; set; }
}

[DataContract]
public class Pj
{
[DataMember]
public string cnpj { get; set; }
[DataMember]
public string corporateName { get; set; }
}

[DataContract]
public class DeliveryAddress
{
[DataMember]
public string neighborhood { get; set; }
[DataMember]
public string city { get; set; }
[DataMember]
public string zipcode { get; set; }
[DataMember]
public string street { get; set; }
[DataMember]
public string additionalInfo { get; set; }
[DataMember]
public string state { get; set; }
[DataMember]
public string number { get; set; }
[DataMember]
public string country { get; set; }
[DataMember]
public string reference { get; set; }
}
[DataContract]
public class Main
{
[DataMember]
public string ddd { get; set; }
[DataMember]
public string number { get; set; }
}
[DataContract]
public class Secondary
{
[DataMember]
public string ddd { get; set; }
[DataMember]
public string number { get; set; }
}
[DataContract]
public class Business
{
[DataMember]
public string ddd { get; set; }
[DataMember]
public string number { get; set; }
}
[DataContract]
public class Telephones
{
[DataMember]
public Main main { get; set; }
[DataMember]
public Secondary secondary { get; set; }
[DataMember]
public Business business { get; set; }
}
[DataContract]
public class Customer
{
[DataMember]
public Pf pf { get; set; }
[DataMember]
public Pj pj { get; set; }
[DataMember]
public DeliveryAddress deliveryAddress { get; set; }
[DataMember]
public Telephones telephones { get; set; }
}
[DataContract]
public class Link
{
[DataMember]
public string id { get; set; }
[DataMember]
public string rel { get; set; }
[DataMember]
public string href { get; set; }
}
[DataContract]
public class Product
{
[DataMember]
public Link link { get; set; }
[DataMember]
public int quantity { get; set; }
[DataMember]
public double price { get; set; }
[DataMember]
public double freight { get; set; }
[DataMember]
public double discount { get; set; }
}
[DataContract]
public class Order
{
[DataMember]
public string id { get; set; }
[DataMember]
public string siteId { get; set; }
[DataMember]
public string purchaseDate { get; set; }
[DataMember]
public string store { get; set; }
[DataMember]
public string lastUpdate { get; set; }
[DataMember]
public string status { get; set; }
[DataMember]
public Invoiced invoiced { get; set; }
[DataMember]
public string estimatedDeliveryDate { get; set; }
[DataMember]
public Customer customer { get; set; }
[DataMember]
public double totalAmount { get; set; }
[DataMember]
public double totalFreight { get; set; }
[DataMember]
public double totalDiscount { get; set; }
[DataMember]
public List<Product> products { get; set; }
}
[DataContract]
public class Pedidos
{
[DataMember]
public List<Order> orders { get; set; }
[DataMember]
public int total { get; set; }
}

Chamada para serializar
#Código
Pedidos pedido = JsonHelper.JsonDeserialize<Pedidos>(lJson);
string xmlPedidos = ObjectToXml<Pedidos>(pedido);

Riechelmann
   - 07 mai 2015

Depois de muito quebrar a cabeça consegui resolver

Eis a solução para o meu problema

#Código
Pedidos pedidos = JsonHelper.JsonDeserialize<Pedidos>(lJson);
int lTotalPedidos = pedidos.orders.Count;

List<Order> pedido = new List<Order>();

for (int i = 0; i < lTotalPedidos; i++)
{
pedido.Add(pedidos.orders[i]);
}

Order[] order = pedido.ToArray();

string xmlPedidos = ObjectToXml<Order[]>(order);