0

I have some results sent by a third party that I must load in my system, this is the XML format that I receive and I must first deserialize it to a class and then store it in a database.

XML format:

<?xml version="1.0" encoding="utf-8"?>
<RESULTADO>
  <FORMULA>
    <EXAMEN>Hematíes</EXAMEN>
    <RESULTADO>4.76</RESULTADO>
    <RANGO_REFERENCIAL>4.8 - 7.1</RANGO_REFERENCIAL>
    <METODO>Colorimetria</METODO>
    <EXAMEN>Hemoglobina</EXAMEN>
    <RESULTADO>13.30</RESULTADO>
    <RANGO_REFERENCIAL> 12.0 -20.0</RANGO_REFERENCIAL>
    <METODO>Cinetico</METODO>
    <EXAMEN>Hematocrito</EXAMEN>
    <RESULTADO>42.00</RESULTADO>
    <RANGO_REFERENCIAL>44 - 60</RANGO_REFERENCIAL>
    <METODO>Cálculo de Histograma</METODO>
    <EXAMEN>VCM</EXAMEN>
    <RESULTADO>88.2</RESULTADO>
    <RANGO_REFERENCIAL>80.0 - 100.0</RANGO_REFERENCIAL>
    <METODO></METODO>
 </FORMULA>
<GLUCOSA>
    <EXAMEN>GLUCOSA</EXAMEN>
    <RESULTADO>87</RESULTADO>
    <RANGO_REFERENCIAL>70 - 110</RANGO_REFERENCIAL>
    <METODO>Enzimatico</METODO>
</GLUCOSA>
<COLESTEROL>
    <EXAMEN>COLESTEROL</EXAMEN>
    <RESULTADO>259</RESULTADO>
    <RANGO_REFERENCIAL>Mayor a  240</RANGO_REFERENCIAL>
    <METODO>Enzimatico</METODO>
    <EXAMEN>Observaciones:</EXAMEN>
    <RESULTADO>Suero lipémico 2 +.</RESULTADO>
    <RANGO_REFERENCIAL></RANGO_REFERENCIAL>
    <METODO></METODO>
</COLESTEROL>
</RESULTADO>

And these are my classes:

Public Class RESULTADO
    <XmlElement("FORMULA")>
    Public Property Numeracion As PRUEBA_LABORATORIO
    <XmlElement("GLUCOSA")>
    Public Property Glucosa As PRUEBA_LABORATORIO
    <XmlElement("COLESTEROL")>
End Class

Public Class PRUEBA_LABORATORIO
    Public Property FILA As ITEM_EXAMEN
End Class

Public Class ITEM_EXAMEN
    <XmlElement(ElementName:="EXAMEN", Order:=1)>
    Public Property EXAMEN As String
    <XmlElement(ElementName:="RESULTADO", Order:=2)>
    Public Property RESULTADO As String
    <XmlElement(ElementName:="RANGO_REFERENCIAL", Order:=3)>
    Public Property RANGO_REFERENCIAL As String
    <XmlElement(ElementName:="METODO", Order:=4)>
    Public Property METODO As String
End Class

As you can see, each group can have one or more "elements" without a main container (EXAMEN + RESULTADO + RANGO_REFERENCIAL + METODO should count as an element of the array) and that's my problem, I can't create the arrays

3
  • 1
    Not sure I follow the question, I don't see anything thing in that XML is looking like an array? Perhaps something has been lost in your explanation because the XML isn't valid and missing tags? Commented Feb 20, 2023 at 20:49
  • I have some results sent by a third party that I must load in my system: Ask the third party to correct their XML. The following may be helpful: stackoverflow.com/a/73640395/10024425 Commented Feb 20, 2023 at 22:06
  • You can use a collection such as generic.List( of RESULTADO) Commented Feb 21, 2023 at 14:56

1 Answer 1

0

As they are company tasks "for yesterday" I had to continue with the trial and error until I found how to read everything:

Imports System.Xml.Serialization

Public Class RESULTADO
    <XmlElement("NUMERACION__FORMULA_Y_PLAQUETAS")>
    Public Property Numeracion As PRUEBA_LABORATORIO
    <XmlElement("GLUCOSA")>
    Public Property Glucosa As PRUEBA_LABORATORIO
    <XmlElement("COLESTEROL")>
    Public Property Colesterol As PRUEBA_LABORATORIO
    <XmlElement("HDL_COLESTEROL")>
    Public Property Colesterol_HDL As PRUEBA_LABORATORIO
    <XmlElement("TRIGLICERIDOS")>
    Public Property Trigliceridos As PRUEBA_LABORATORIO
    <XmlElement("SEROLOGICAS_CUALITATIVAS")>
    Public Property Serologicas As PRUEBA_LABORATORIO
    <XmlElement("ANTIGENO_PROSTATICO_ESPECIFICO_PSA")>
    Public Property AntigenoPSA As PRUEBA_LABORATORIO
    <XmlElement("EXAMEN_COMPLETO_DE_ORINA")>
    Public Property Orina As PRUEBA_LABORATORIO
    <XmlElement("THEVENON")>
    Public Property Thevenon As PRUEBA_LABORATORIO
    <XmlElement("PERFIL_LIPIDICO_COMPRENDE_COLESTEROLHD")>
    Public Property PerfilLipidico As PRUEBA_LABORATORIO
End Class

Public Class PRUEBA_LABORATORIO
    <XmlElement(ElementName:="EXAMEN")>
    Public Property EXAMEN As String()
    <XmlElement(ElementName:="RESULTADO")>
    Public Property RESULTADO As String()
    <XmlElement(ElementName:="RANGO_REFERENCIAL")>
    Public Property RANGO_REFERENCIAL As String()
    <XmlElement(ElementName:="METODO")>
    Public Property METODO As String()
End Class

And when reading the file:

Dim reader As New System.Xml.Serialization.XmlSerializer(GetType(RESULTADO))
Dim file As New IO.StreamReader("C:\dev\2023007720.xml")
Dim overview As RESULTADO = CType(reader.Deserialize(file), RESULTADO)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.