[英]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: xml像这样被序列化:
<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. 我找不到任何Xml属性,可以在序列化过程中放置一个属性来“删除”第一个Produto节点。
Any ideas on how to accomplish this? 关于如何做到这一点的任何想法?
Thank you. 谢谢。
Just use the attribute XmlElement in your List property. 只需在List属性中使用属性XmlElement。 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输出
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.