ASP.NET 2.0 Web 服务中的 XML 生成?

发布于 2024-10-28 22:00:58 字数 1115 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦年海沫深 2024-11-04 22:00:58

XML 序列化仅适用于公共、读/写属性。抱歉,您的只读属性永远不会被序列化。

XML Serialization only works with public, read/write properties. Sorry, but your read-only properties will never be serialized.

‘画卷フ 2024-11-04 22:00:58

ASP.NET 2.0 Web 服务运行 System.Xml.Serialization 中的 XML Serializer(如果没记错的话)以及其中的相关属性,例如 XmlIgnore()。

不幸的是,让它与多态集合一起工作可能有点棘手。如果类的数量有限,最简单的方法是执行以下操作:

public class Shelf
{
    public Bottle[] Bottles {get; set;}
    public Box[] Boxes {get; set;}
}

这里可能值得查看 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:

public class Shelf
{
    public Bottle[] Bottles {get; set;}
    public Box[] Boxes {get; set;}
}

It might be worth looking at WCF here, it has much better options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文