Maturano
Maturano

Reputation: 1023

C# Xml Serialization nodes

I have these classes

public class ProdutosServicos
{           
    public List<Produto> Produto { get; set; }  
}

public class Produto
{
    public string Descricao { get; set; }        
    public CodigoTipo Codigo { get; set; }
    public string Quantidade { get; set; }
    public string Unidade { get; set; }
    public string ValorUnitario { get; set; }
}

ANd the xml is being serialized like this:

<ProdutosServicos>
   <Produto>
      <Produto>
        ...
      </Produto>
   </Produto>
</ProdutosServicos>

But i would like to generate like this:

<ProdutosServicos>
   <Produto>
    ...
   </Produto>
</ProdutosServicos>

I couldn't find any Xml Attribute to put on a property to "Remove" the first Produto node during the serialization.

Any ideas on how to accomplish this?

Thank you.

Upvotes: 2

Views: 1689

Answers (2)

Emmanuel Ponnudurai
Emmanuel Ponnudurai

Reputation: 1074

Please see below solution,

Class definition,

[Serializable]
[XmlRoot("ProdutosServicos")]
public class ProdutosServicos
{
    [XmlElement("Producto")]
    public List<Produto> Produto { get; set; }
}

[Serializable]
public class Produto
{
    public string Descricao { get; set; }
    public CodigoTipo Codigo { get; set; }
    public string Quantidade { get; set; }
    public string Unidade { get; set; }
    public string ValorUnitario { get; set; }
}
[Serializable]
public class CodigoTipo
{

}

Code to serialize,

XmlSerializer serializer = new XmlSerializer(typeof(ProdutosServicos));
        var productoServices = new ProdutosServicos();
        Produto producto1 = new Produto();
        producto1.Descricao = "Descricao1";
        producto1.Quantidade = "Quantidade1";
        Produto producto2 = new Produto();
        producto2.Descricao = "Descricao2";
        producto2.Quantidade = "Quantidade2";

        List<Produto> collectionProducto = new List<Produto>();
        collectionProducto.Add(producto1);
        collectionProducto.Add(producto2);
        productoServices.Produto = collectionProducto;

        string xmlString = string.Empty;
        using (StringWriter stringWriter = new StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(stringWriter))
            {
                serializer.Serialize(writer, productoServices);
                //String in required format
                xmlString = stringWriter.ToString(); 
            }
        }

XML Output

<?xml version="1.0" encoding="utf-16"?>
<ProdutosServicos xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Producto>
    <Descricao>Descricao1</Descricao>
    <Quantidade>Quantidade1</Quantidade>
  </Producto>
  <Producto>
    <Descricao>Descricao2</Descricao>
    <Quantidade>Quantidade2</Quantidade>
  </Producto>
</ProdutosServicos>

Upvotes: 1

Ismael
Ismael

Reputation: 622

Just use the attribute XmlElement in your List property. Like this.

public class ProdutosServicos
{
    // This is the attribute that makes your XML Array looks like a list of XML Elements.
    [XmlElement]
    public List<Produto> Produto { get; set; }
}

The result will be something like this:

<?xml version="1.0" encoding="utf-16"?>
<ProdutosServicos xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Produto>
    ...
  </Produto>
  <Produto>
    ...
  </Produto>
  <Produto>
    ...
  </Produto>
</ProdutosServicos>

Upvotes: 1

Related Questions