Why does xmlwritter omit data when saving in xml?

-1

I have several objects in a class from an xsd file and I have the following object to give an example:

    FacturaElectronicaResumenFactura resumen = new 
    FacturaElectronicaResumenFactura()
        {

            CodigoMoneda = FacturaElectronicaResumenFacturaCodigoMoneda.CRC,
            TipoCambio = 1m,
            TotalServExentos = 1m,
            TotalMercanciasGravadas = 1m,
            TotalMercanciasExentas = 2000m,
            TotalGravado = 1m,
            TotalExento = 2000m,
            TotalVenta = 2000m,
            TotalDescuentos = 1m,
            TotalVentaNeta = 2000m,
            TotalImpuesto = 1m,
            TotalComprobante = 2000m

        };

And I want to save it in an xml for which I have the following code:

    XmlSerializer serial = new XmlSerializer(typeof(FacturaElectronica));
    var ruta = 
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + 
    "//SerializationOverview.xml";
    System.IO.FileStream file = System.IO.File.Create(ruta);
    serial.Serialize(file,fe);
    file.Close();

but when I open the xml to review, some data or properties defined in the invoice class are not saved. Because you are not saving all the properties of the object in the xml file, any ideas ???

    
asked by Richard Víquez Pérez 10.10.2018 в 05:18
source

2 answers

0

Possibly the class generated through the XSD file does not have all the properties necessary to do the serialization properly, such as SerializableAttribute, XmlTypeAttribute and / or XmlRootAttribute. To make sure of this, Microsoft made a tool that converts the XSD to a class with everything necessary (skip this part if you already did it):

  • Execute the command in your windows console to go to the directory where the xsd.exe application is located. Depending on the version you have of VS this directory may vary.
  •   

    cd C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v10.0A \ bin \ NETFX 4.6.1 Tools

  • Execute the command to convert the XSD into a class (this command has variants and you can even generate it in C #, for your code I'm going to leave it in VB). I'm taking into account that your file is FacturaElectronicaResumenFactura.xsd and is in the directory E: / xsd /
  •   

    xsd.exe E: \ xsd \ InvoiceElectronicaResumenFactura.xsd / classes / language: vb / out: E: \ xsd \

    With this you get a class in the same name in the same directory with extension .vb.

    Now for the serialization, I have done it with the following code. Which also uses override of the encoding generated by the StringWriter and set it as utf-8

    Private Function ParseFacturaToXML(ByVal _factura As FacturaElectronicaResumenFactura ) As String
        'Serializa el objeto en un string xml
        Dim serializer As New XmlSerializer(GetType(FacturaElectronicaResumenFactura ))
        Dim textWriter As New Utf8StringWriter()
        serializer.Serialize(textWriter, _factura)
        Dim sXML As String = textWriter.ToString()
    
        Return sXML 
    End Function
    
    Public Class Utf8StringWriter
        Inherits StringWriter
    
        Public Overrides ReadOnly Property Encoding As Encoding
            Get
                Return System.Text.Encoding.UTF8
            End Get
        End Property
    End Class
    

    In the output of the ParseFacturaToXML method you should see the XML string and you can use it to save it in a file as you were doing.

        
    answered by 10.10.2018 в 18:44
    0

    @Salvador Navano worked as you will continue omitting nodes         -

         -<LineaDetalle>
    
         <NumeroLinea>1</NumeroLinea>
    
        <Cantidad>1</Cantidad>
    
        <UnidadMedida>kg</UnidadMedida>
    
        <Detalle>detalle</Detalle>
    
        <PrecioUnitario>2000</PrecioUnitario>
    
       <MontoTotal>2000</MontoTotal>
    
       <SubTotal>2000</SubTotal>
    
       <MontoTotalLinea>2000</MontoTotalLinea>
    
       </LineaDetalle>
    
       </DetalleServicio>
    
    
       -<ResumenFactura>
    
       <TotalVenta>2000</TotalVenta>
    
       <TotalVentaNeta>2000</TotalVentaNeta>
    
       <TotalComprobante>2000</TotalComprobante>
    
        </ResumenFactura>
    
        
    answered by 12.10.2018 в 05:06