API - xml响应格式

时间:2015-11-27 11:07:13

标签: asp.net xml vb.net asp.net-web-api

您好我创建一个api来获取ID Xml,如下所示 响应格式

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

您可以在下面找到我定义的模型

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

然后在我使用

的源代码下面
    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

结束如果

响应没有<post>的问题,如下例所示

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

1 个答案:

答案 0 :(得分:0)

更改sql查询可能是您的解决方案。在查询下面,在xml中添加一个帖子节点。 试试这个

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