ASP.NET 2.0 Web 服务中的 XML 生成?
我正在使用 ASP 2.0 构建一个 Web 服务 (.asmx),并且我的许多方法都返回自定义对象。它总是使用对象的属性将我的对象转换为 XML,但我遇到了不这样做的情况。
我需要一次返回一堆不同类的对象,不幸的是这些类没有通过继承关联,所以我从我的 Web 方法返回一个对象数组。输出如下所示:
<ArrayOfAnyType>
<anyType xsi:type="Type1"/>
<anyType xsi:type="Type2"/>
</ArrayOfAnyType>
Type1 和 Type2 类已定义属性,但它们不是自动实现的,并且是只读的。到目前为止,我所看到的自动转换为 XML 的所有属性都已完全自动实现。这就是它不能正确转换的原因吗?我是否必须重新设计我的类才能使其正常工作,或者是否可以在某处添加一个属性,或者可以实现一个接口,或者类似的东西?
我的类声明如下所示:
Public Class Type1
Dim _var1 As Decimal
Public Sub New()
Dim conn As New SqlConnection(ConfigurationManager.AppSettings("myString"))
conn.Open()
Dim command As New SqlCommand("SELECT * FROM table1", conn)
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
_var1 = reader("Var1")
reader.Close()
conn.Close()
End Sub
Public ReadOnly Property Var1() As Decimal
Get
Return _var1
End Get
End Property
End Class
编辑:澄清我的问题:为什么 XML 序列化过程忽略我在此类中的属性?因为它们不是自动实现的?或者因为它们是只读的?或者其他什么?
I'm building a web service (.asmx) with ASP 2.0, and a lot of my methods return custom objects. It's always converted my objects into XML for me using the properties of the objects, but I've run into a situation where it doesn't.
I need to return a bunch of objects of different classes at once, and the classes are unfortunately not related through inheritance, so I'm returning an array of objects from my web method. The output looks like this:
<ArrayOfAnyType>
<anyType xsi:type="Type1"/>
<anyType xsi:type="Type2"/>
</ArrayOfAnyType>
The Type1 and Type2 classes have properties defined, but they're not auto-implemented, and they're read only. All properties I've seen auto-converted into XML so far have been fully auto-implemented. Is this why it doesn't convert properly? Am I going to have to redesign my classes to get this to work, or is there an attribute I can add somewhere, or an interface I can implement, or something like that?
My class declarations look like this:
Public Class Type1
Dim _var1 As Decimal
Public Sub New()
Dim conn As New SqlConnection(ConfigurationManager.AppSettings("myString"))
conn.Open()
Dim command As New SqlCommand("SELECT * FROM table1", conn)
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
_var1 = reader("Var1")
reader.Close()
conn.Close()
End Sub
Public ReadOnly Property Var1() As Decimal
Get
Return _var1
End Get
End Property
End Class
EDIT: clarifying my question: why is the XML serialization process ignoring my properties in this class? Because they're not auto-implemented? Or because they're read only? Or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 序列化仅适用于公共、读/写属性。抱歉,您的只读属性永远不会被序列化。
XML Serialization only works with public, read/write properties. Sorry, but your read-only properties will never be serialized.
ASP.NET 2.0 Web 服务运行 System.Xml.Serialization 中的 XML Serializer(如果没记错的话)以及其中的相关属性,例如 XmlIgnore()。
不幸的是,让它与多态集合一起工作可能有点棘手。如果类的数量有限,最简单的方法是执行以下操作:
这里可能值得查看 WCF,它有更好的选择。
ASP.NET 2.0 web services run off the XML Serializer in System.Xml.Serialization (if memory serves) and the related attributes there such as XmlIgnore().
Unfortunately, getting that to work with a polymorphic collection can be a bit of a trick. If it is a limited number of classes, the easiest way out would be to do something like:
It might be worth looking at WCF here, it has much better options.