【发布时间】: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