简体   繁体   中英

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.

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM