VB.net, Conversion XML format stored in Access

0

I'm doing a program in .net, which should bring some Access data and save it in a file, among these data is an XML, which changes the format when saving it in a file. Code to search the XML:

  Public Function Buscar_XML()

    conecta()

    sql = New OleDbCommand("SELECT XML FROM TNR where TNR_NAME='" & Form1.ListBox1.SelectedItem.ToString & "'", cnn)
    Reader = sql.ExecuteReader()

    Do While (Reader.Read())
        Form1.XML = Reader.GetValue(0)
    Loop


    Reader.Close()

    desconectar()
End Function

Create the file:

  My.Computer.FileSystem.WriteAllText("C:\TNR\PRUEBA.xml", XML, False)

XML saved in Access (I copy a small fragment as an example)

<body>
 <ENVELOPE>
  <OP_MSG_TYPE>SO</OP_MSG_TYPE>
  <OP_OPERATION>PUBLISH</OP_OPERATION>

And this is what you save in the .xml file:

<div>&lt;body&gt;</div>

<div>   &lt;ENVELOPE&gt;</div>

<div>       &lt;OP_MSG_TYPE&gt;SO&lt;/OP_MSG_TYPE&gt;</div>

<div>       &lt;OP_OPERATION&gt;PUBLISH&lt;/OP_OPERATION&gt;</div>

How can I do to take the value as it is in the BD, and why is this happening?

Thank you very much

    
asked by Cristian Raña 10.06.2016 в 15:42
source

1 answer

0

You should serialize a class

Public Class ENVELOPE

   Public Property OP_MSG_TYPE As String
   Public Property OP_OPERATION As String

End Class

The idea is to serialize the xml by assigning the data to the class

Dim obj As New ENVELOPE

Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(ENVELOPE))

Dim myWriter As StreamWriter = New StreamWriter("myFileName.xml")
mySerializer.Serialize(myWriter, myObject)
myWriter.Close()

You could generate the xml in memory with a MemoryStream instead of the StreamWriter and then send it to the db

In your code you have to use parameters

Public Function Buscar_XML()

    conecta()

    Dim sql As New OleDbCommand("SELECT XML FROM TNR where TNR_NAME= @name", cnn)
    sql.Parameters.AddWithValue("@name", Form1.ListBox1.SelectedItem.ToString)
    Dim reader As SqlDataReader = sql.ExecuteReader()

    Do While (reader.Read())
        Form1.XML = reader.GetValue(0)
    Loop

    reader.Close()

    desconectar()

End Function
    
answered by 10.06.2016 в 16:03