2

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.

Maturano
  • 951
  • 4
  • 15
  • 42
  • 1
    Your POCO class represents exactly what you have in your XML - if you are serializing the POCO the resulting XML is indeed right. So the question is - is the `Produto a collection`? if yes all you can have is set an attribute name - a plural form `Produtos` then you will have the following structure `...` . Probably this might help you http://stackoverflow.com/questions/14967293/remove-parent-node-without-childs-nodes – Jaya Apr 13 '16 at 20:39
  • @JS_GodBlessAll, yeah, Produto is a collection, i know if i pluralize the name it will became , but the requirement is to have only =/, exactly the way i've put above. – Maturano Apr 14 '16 at 11:53

2 Answers2

1

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>
Ismael
  • 622
  • 6
  • 11
1

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>
  • @Maturano OK. Good to know. Also you can check the various ways of XML serialization and manipulating the result XML as per you needs. You can start here [Introducing XML Serialization](https://msdn.microsoft.com/en-us/library/182eeyhh(v=vs.110).aspx) – Emmanuel Ponnudurai Apr 14 '16 at 14:13