在.NET中的XML反序列化,我怎么才能让多态使用数组类型的? [英] In .NET XML deserialization, how can I allow polymorphic use of Array types?

查看:134
本文介绍了在.NET中的XML反序列化,我怎么才能让多态使用数组类型的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例模式:

<complexType name="Dog">...</complexType>
<complexType name="Cat">...</complexType>

<complexType name="ArrayOfDog">
    <sequence>
        <element name="Dog" type="tns:Dog minOccurs="0" maxOccurs="unbounded" />
    </sequence>
</complexType>

<complexType name="Foo">
    <sequence>
        <element name="Bar" type="string"/>          
        <element name="Baz" type="anyType"/>
    </sequence>
</complexType>

通过.NET的Wsdl.exe用运行此产生code类似以下内容:

Running this through .NET's wsdl.exe generates code similar to the following:

[System.Xml.Serialization.XmlIncludeAttribute(typeof(Dog[]))]

public partial class Dog { ... }

public partial class Cat { ... }

public partial class Foo {
    private string barField;
    private object bazField;
}

看来,Wsdl.exe用正努力成为智能,实现我的ArrayOfDog真的只是可以连接codeD为C#阵列封装类型。这正常工作时ArrayOfDog在另一个数据类型的显式引用。然而,当ArrayOfDog是多态地使用(例如,作为替代XSD:anyType的),这打破。它的出现打破,因为.NET运行库一无所知名为ArrayOfDog的复杂类型 - 它已经基本扔掉这些信息有利于刚使用原生的C#阵列

It appears that wsdl.exe is trying to be "smart" and realize that my ArrayOfDog is really just a wrapper type that can be encoded as a C# array. This works fine when ArrayOfDog is explicitly referenced in another data type. However, when ArrayOfDog is used polymorphically (e.g. as a substitution for xsd:anyType) this breaks. It appears to break because the .NET runtime knows nothing about the complexType named "ArrayOfDog" - it has basically thrown away this information in favor of just using native C# arrays.

示例XML文档1:

<Foo>
    <Bar>Hello</Bar>
    <Baz xsi:type="Cat">
        ...
    </Baz>
</Foo>

示例XML文档2:

Example XML document 2:

<Foo>
    <Bar>Hello</Bar>
    <Baz xsi:type="ArrayOfDog">
        <Dog>...</Dog>
        <Dog>...</Dog>
    </Baz>
</Foo>

文件#1是正确运行时反序列化。我得到Foo类型的对象与酒吧和巴兹正确的反序列化的字段。

Document #1 is deserialized correctly by the runtime. I get an object of type Foo with correctly deserialized fields for Bar and Baz.

文献2反序列化的错误运行时。我得到Foo类型的对象有一个正确的反序列化的领域吧,但对于巴兹领域,我得到System.XML.XMLNode []。我的猜测是因为运行时一无所知任何类型的名为ArrayOfDog的实体结合。你可能会认为XmlInclude指令XmlIncludeAttribute(typeof运算(狗[]))会处理这个问题,但它似乎并没有奏效。

Document #2 is deserialized incorrectly by the runtime. I get an object of type Foo with a correctly deserialized field for Bar, but for the Baz field I get System.XML.XMLNode[]. My guess is because the runtime knows nothing about any type binding for an entity named "ArrayOfDog". You might think that the XmlInclude directive "XmlIncludeAttribute(typeof(Dog[]))" would handle this, but it doesn't appear to be working.

有没有人遇到过这个?

有一种优雅的解决方案吗?我想用的解决方法是换另一种类型的我ArrayOf类型,包括在subsitution为XSD:anyType的

Is there an elegant solution here? The workaround I'm thinking of using is to wrap my "ArrayOf" type in another type and include that in the subsitution for the xsd:anyType.

推荐答案

我不认为这有什么关系多态性。我认为这是在XML序列中的错误,假设名为ArrayOfDog,含有狗的序列中的任何类型的目的是重新present狗[]。由于这一理论的测试,请尝试更改WSDL中使用的名称BunchOfDogs来代替,看看这改变了代理code客户端。

I don't think this has anything to do with polymorphism. I think this is a bug in the XML Serializer, assuming that any type named "ArrayOfDog", containing a sequence of "Dog" is meant to represent a Dog[]. As a test of this theory, try changing the WSDL to use the name "BunchOfDogs" instead, and see if this changes the proxy code in the client.

如果你想多态性的XML,则两个ArrayOfDog,和猫,将需要相同的基本类型(比其他的xsd:任何)的扩展。如果是这样的话,那么我期望.NET生成巴兹作为基本类型的。

If you want polymorphism in XML, then both ArrayOfDog, and Cat, will need to be extensions of the same base type (other than xsd:any). If that were the case, then I'd expect .NET to generate Baz as being of the base type.

架构与XSD:任何原因引起的问题只是一般。有可能是在那里几乎所有的东西,和一些组合会根本就没有意义了。

Schemas with xsd:any cause problems just in general. There could be almost anything in there, and some combinations will simply not make sense.

您还没有说,如果这个Java服务是安讯士,或者是什么版本,它是。我见过轴运行起来的xsi:type是一个替代的有效模式。小心需要正确使用XSI模式:类型

You haven't said if this Java service is from Axis, or what version it is. I've seen Axis behave as though xsi:type were a substitute for a valid schema. Be careful with schemas that require "proper" use of xsi:type.

这篇关于在.NET中的XML反序列化,我怎么才能让多态使用数组类型的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆