Android Question Reading xml from DataTable exported via Web Services (asp.net enviroment)

Bobi

Member
Licensed User
Longtime User
With simple code shown below I received via Web Services result as you can see in attach file. I don't understand how to read structure/result in this case.

Thanks!

P.S. I don't know if it's a good approach but if I use...
B4X:
Return ds.GetXml
return is string not xml (
B4X:
Public Function fn(PublicKey As String) As String
) as shown in second attach.

B4X:
 <WebMethod()>
<ScriptMethod(UseHttpGet:=True)> _
    Public Function fn(PublicKey As String) As DataTable
        Dim parts As String() = PublicKey.Split(New Char() {"~"c})


        Dim connStringOAA As String
        connStringOAA = "xxx"

        Dim command As SqlCommand
        Dim conn As New SqlConnection(connStringOAA)
        Dim da As SqlDataAdapter = Nothing
        Dim ds As DataSet = Nothing
        conn.Open()
        Try
            command = New SqlCommand("fn", conn)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.AddWithValue("@Device_ID", parts(0))
            da = New SqlDataAdapter(command)
            ds = New DataSet("fn")
            da.Fill(ds)
        Catch ex As Exception

        Finally
            command = Nothing
            conn.Close()
        End Try
        Dim dt As DataTable = ds.Tables(0)

        Return dt
    End Function
 

Attachments

  • xmlResult.xml
    3.1 KB · Views: 150
  • xmlResult2.xml
    1.5 KB · Views: 137

DonManfred

Expert
Licensed User
Longtime User
It does not look like an B4A Code
Remember you are in B4A-forum here. Maybe you need to post a thread in chit chat forum as it is not related to b4a
 
Upvote 0

Bobi

Member
Licensed User
Longtime User
Upvote 0

Bobi

Member
Licensed User
Longtime User
"raise the stakes":

I found interesting code wich convert DataTable (.net) into JSon format:

B4X:
Public Function GetJson(ByVal dt As DataTable) As String
        Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim rows As New List(Of Dictionary(Of String, Object))
        Dim row As Dictionary(Of String, Object)
        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName, dr(col))
            Next
            rows.Add(row)
        Next
        Return serializer.Serialize(rows)
    End Function

Here is much more simply because result is JSon but... in xml container as unique record. Feel free to test.

P.S. Excuse my English please
 
Upvote 0

Bobi

Member
Licensed User
Longtime User
Thanks for your support.
Looks better now?

EDITED:
airblaster ask:
"If you are parsing different XML files, can you reuse the parser object? Or is it safer to create a parser object for each XML file?"
Erel response was:
"It is better to create a new object."
http://www.basic4ppc.com/android/forum/threads/xml-parsing-with-the-xmlsax-library.6866/page-5

Using this solution with xml from .net web services could simplify somehow entire solution because we have only one xml record with all JSon string. Right?
 

Attachments

  • ResultAsJSon.txt
    1.1 KB · Views: 149
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Using this solution with xml from .net web services could simplify somehow entire solution because we have only one xml record with all JSon string. Right?

It´s easier but the result is NOT a plain JSON... It is a JSON embedded in some kind of mini-xml-format.

I think the values have to be unescaped but
<string xmlns="http://tempuri.org/">
[{"DataLivrare":"\/Date(1384725600000)\/","Livrata":null},{"DataLivrare":"\/Date(1380315600000)\/","Livrata":null},{"DataLivrare":"\/Date(1377032400000)\/","Livrata":null},{"DataLivrare":"\/Date(1371675600000)\/","Livrata":null},{"DataLivrare":"\/Date(1369083600000)\/","Livrata":null},{"DataLivrare":"\/Date(1367442000000)\/","Livrata":null},{"DataLivrare":"\/Date(1365541200000)\/","Livrata":null},{"DataLivrare":"\/Date(1363644000000)\/","Livrata":null},{"DataLivrare":"\/Date(1362348000000)\/","Livrata":null},{"DataLivrare":"\/Date(1360792800000)\/","Livrata":null},{"DataLivrare":"\/Date(1358892000000)\/","Livrata":true},{"DataLivrare":"\/Date(1356040800000)\/","Livrata":true},{"DataLivrare":"\/Date(1353362400000)\/","Livrata":true},{"DataLivrare":"\/Date(1351112400000)\/","Livrata":true},{"DataLivrare":"\/Date(1348002000000)\/","Livrata":true},{"DataLivrare":"\/Date(1345150800000)\/","Livrata":true},{"DataLivrare":"\/Date(1342818000000)\/","Livrata":true},{"DataLivrare":"\/Date(1340139600000)\/","Livrata":true}]
</string>
getting just the JSON from this results in
[{"DataLivrare":"\/Date(1384725600000)\/","Livrata":null},{"DataLivrare":"\/Date(1380315600000)\/","Livrata":null},{"DataLivrare":"\/Date(1377032400000)\/","Livrata":null},{"DataLivrare":"\/Date(1371675600000)\/","Livrata":null},{"DataLivrare":"\/Date(1369083600000)\/","Livrata":null},{"DataLivrare":"\/Date(1367442000000)\/","Livrata":null},{"DataLivrare":"\/Date(1365541200000)\/","Livrata":null},{"DataLivrare":"\/Date(1363644000000)\/","Livrata":null},{"DataLivrare":"\/Date(1362348000000)\/","Livrata":null},{"DataLivrare":"\/Date(1360792800000)\/","Livrata":null},{"DataLivrare":"\/Date(1358892000000)\/","Livrata":true},{"DataLivrare":"\/Date(1356040800000)\/","Livrata":true},{"DataLivrare":"\/Date(1353362400000)\/","Livrata":true},{"DataLivrare":"\/Date(1351112400000)\/","Livrata":true},{"DataLivrare":"\/Date(1348002000000)\/","Livrata":true},{"DataLivrare":"\/Date(1345150800000)\/","Livrata":true},{"DataLivrare":"\/Date(1342818000000)\/","Livrata":true},{"DataLivrare":"\/Date(1340139600000)\/","Livrata":true}]

And this can be parsed with

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
Dim DataLivrare As String = colroot.Get("DataLivrare")
Dim Livrata As String = colroot.Get("Livrata")
Next

you can use httputils2 to call the webservice.
Extract the JSON from result
and then feed the jsonparser with this json
 
Upvote 0

Bobi

Member
Licensed User
Longtime User
It´s easier but the result is NOT a plain JSON... It is a JSON embedded in some kind of mini-xml-format.

I think the values have to be unescaped but

getting just the JSON from this results in


And this can be parsed with

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
Dim DataLivrare As String = colroot.Get("DataLivrare")
Dim Livrata As String = colroot.Get("Livrata")
Next

you can use httputils2 to call the webservice.
Extract the JSON from result
and then feed the jsonparser with this json

Nice! That's all folks! I have all pieces in the right place now. Thanks again.
 
Upvote 0
Top