【问题标题】:Retrieving primitive data type from WebService in VB .NET从 VB .NET 中的 WebService 检索原始数据类型
【发布时间】:2013-11-12 14:53:51
【问题描述】:

我已经在 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

然后我创建一个产品对象列表:

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 字段(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

where table 包含我的 SQL 查询的结果

【问题讨论】:

  • 请在您减少返回字段数量的地方发布您的网络服务代码,特别是您给出的示例,当您只期望两个字段时,您会返回四个字段。
  • 我正在简化让我班级的所有成员都未分配(idProduct 和 state 除外)
  • 您通过 XmlSerializer 获得这些字符串?

标签: .net vb.net web-services


【解决方案1】:

将可以省略的字段设为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

现在idCategory 和price 将默认为Nothing(即null)。

【讨论】:

  • 谢谢@Karl,但是客户端应该总是调用同一个WS,是否检索所有产品详细信息或仅检索id和状态的逻辑取决于状态值(存储在数据库中) .所以,我可以混合“类型”的 xml 产品
  • @Joseph82 - 使idCategory 和price Nullable(Of) 不起作用?
  • 我在 p.price=row.Item("Price") 上得到一个错误。 “指定的演员表无效”。而对于整数文件,我在 xml 中获得:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 2020-02-12
  • 1970-01-01
相关资源
最近更新 更多