简体   繁体   English

API - xml 响应格式

[英]API - xml response formatting

Hello I'm creating an api to get id and name as Xml like this你好,我正在创建一个api来像这样获取 id 和名称为Xml
Response Formats响应格式

  <posts>
  <post>
  <account_id> </account_id>
  <name> </name>
  </post>
  </posts>

You can find below what models I define您可以在下面找到我定义的模型

Partial Public Class PostsPost

    Private postField As Posts

    Public Property Post() As Posts
        Get
            Return Me.postField
        End Get
        Set
            Me.postField = Value
        End Set
    End Property
End Class


  Partial Public Class Posts



    Private account_idField As String

    Private nameField As String




    Public Property Account_id() As String
        Get
            Return Me.account_idField
        End Get
        Set
            Me.account_idField = Value
        End Set
    End Property


    Public Property Name() As String
        Get
            Return Me.nameField
        End Get
        Set
            Me.nameField = Value
        End Set
    End Property


 End Class

Then below the source code that I'm using然后在我正在使用的源代码下方

    Try myConnection.ConnectionString = "Data Source = xxx.xxx.x.yy;port=1234;Database=test;User ID=test;Password=test123;"
            If myConnection.State = ConnectionState.Closed Then
                myConnection.Open()
            End If

            Dim reader As MySqlDataReader = Nothing
            Dim mysqlCmd As New MySqlCommand()
            mysqlCmd.CommandType = CommandType.Text
            mysqlCmd.CommandText = "Select account_id,name from xx where  account_id Like ?p1 and xylike ?p2 "
            mysqlCmd.Parameters.AddWithValue("?p1", xxx)
            mysqlCmd.Parameters.AddWithValue("?p2", yyy)
            mysqlCmd.Connection = myConnection
            reader = mysqlCmd.ExecuteReader()
            If reader.HasRows Then
                Dim smtSub As Posts = Nothing
                While reader.Read()
                    smtSub = New Posts()
                    smtSub.Post.Account_id = Convert.ToInt32(reader.GetValue(0))
                    smtSub.Post.Name = reader.GetValue(1).ToString()

                End While
                myConnection.Close()
  return smtSub

End If万一

The problem that the response is without the <post> like the example below响应没有<post>的问题,如下例所示

   <posts>
    <account_id> </account_id>
    <name> </name>
     </posts>

Change sql query may be your solution.更改 sql 查询可能是您的解决方案。 Below query add a post node in your xml.下面的查询在您的 xml 中添加一个 post 节点。 Try this尝试这个

 mysqlCmd.CommandText = "select xml_tag('Posts','Post',null,null) from xx where  account_id Like ?p1 and xylike ?p2 "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM