在VB .NET中从WebService检索原始数据类型

时间:2013-11-12 14:53:52

标签: .net vb.net web-services

我在VB .NET中实现了一个Web服务。

为了检索数据,我创建了一个这样的对象:

Public Class Product 
    Public idProduct As Integer
    Public state As Integer
    Public description As String
    Public longDescription As String
    Public idCategory As Integer
    Public price As Decimal
End Class

然后我创建了一个Product对象列表:

Dim ProductList As List(Of Product)

这就是我的WebMethod返回的内容。

Web服务运行良好,并返回一个包含此类产品列表的xml

<Product>
   <idProduct>169</idProduct >
   <state>0</state>
   <idCategory>32001</idCategory>
   <description>description of the article</description>
   <longDescription>extended description of the article</longDescription>
   <price>2.44</price>
</Product>

但是,在某些情况下,我不想检索所有这些信息。我想要返回idProduct和状态:

<Product>
   <idProduct>170</idProduct >
   <state>-1</state>
</Product>

相反,我获得的是以下内容:

<Product>
   <idProduct>170</idProduct >
   <state>-1</state>
   <idCategory>0</idCategory>
   <price>0</price>
</Product>

只有String fileds(description和longDescription)从xml中删除,而对象中的所有字段都被定义为Integer或Decimal,如果我没有设置任何值,则自动设置为零。

修改

我用于创建对象的代码如下:

For Each row In table.Rows
   p = New Product()
   p.idProduct= Utilities.DBNullToInvalidInt(row.Item("ID"))
   p.state= Utilities.DBNullToInvalidInt(row.Item("State"))
   If p.FileStateID = 0 Then
      p.idCategory= Utilities.DBNullToInvalidInt(row.Item("idCategory"))
      p.description= Utilities.DBNullToVoidString(row.Item("Description"))
      p.longDescription = Utilities.DBNullToVoidString(row.Item("ExtendedDescription"))
      p.price= row.Item("Price")
   End If

   ProductList .Add(p)

Next

其中table包含我的SQL查询的结果

1 个答案:

答案 0 :(得分:0)

制作可能省略的字段Nullable,如下所示:

Public Class Product 
    Public idProduct As Integer
    Public state As Integer
    Public description As String
    Public longDescription As String
    Public idCategory As Nullable(Of Integer)
    Public price As Nullable(Of Decimal)
End Class

现在idCategoryprice默认为Nothing(即null)。

相关问题