在 WCF 中使用 XmlSerializer 序列化 IXmlSerializable 类型 [英] Serializing IXmlSerializable Types Using XmlSerializer in WCF

查看:22
本文介绍了在 WCF 中使用 XmlSerializer 序列化 IXmlSerializable 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对 DataContractSerializer 感到非常沮丧,我正在尝试使用 IXmlSerializable 类型和 XmlSerializer 在 WCF 中启动并运行.

Thoroughly frustrated with the DataContractSerializer, I'm trying to get up and running in WCF using IXmlSerializable types with XmlSerializer.

我已经在我的类中实现了 IXmlSerializable XmlSchemaProvider 以进行序列化并为我的 OperationContract 声明 [XmlSerializerFormat].

I've implemented IXmlSerializable and XmlSchemaProvider in my class to be serialized and declared [XmlSerializerFormat] for my OperationContract.

使用复杂的架构,我在尝试查看 WSDL 时遇到以下错误:

Using a complex schema, I get the following error on trying to view the WSDL:

"Schema type information provided by IXmlSerializable is invalid: 
Reference to undeclared attribute group 'anAttributeGroupInMySchema'"

模式有各种包含(此属性在其中之一中声明).我什至在代码中添加了包含的模式 (schema.Includes) 但无济于事.

The schema has various includes (this attribute is declared in one of them). I even added the included schemas in code (schema.Includes) but to no avail.

即使在最简单的示例项目中(具有 1 个元素和 2 个属性的简单架构,简单的对应类,您可以命名)我也终于克服了这个错误,但直接遇到了:

Even in the most trivial example project (simple schema with 1 element and 2 attributes, simple corresponding class, you name it) I finally get past this error, but bump right into:

"WCF_IXmlSerializable.TestClass.TestSchemaProvider() must return a valid type 
name. Type 'TEST' cannot be found in the targetNamespace='www.test.com'."

遗憾的是,我不知道什么是有效的类型名称​​.它肯定不是来自我的 XSD、AFAICS 的元素名称.

Sadly I don't know what is a valid type name. It's certainly not an element name from my XSD, AFAICS.

有什么想法吗?

示例源代码可以在此处在线查看.

Example source code can be viewed online here.

推荐答案

我看到两个问题:您的测试架构没有定义名为 TEST_CLASS 的类型,它定义了一个具有该名称的元素.XSD 应该是这样的:

I see two problems: your test schema does not define a type named TEST_CLASS, it defines an element with that name. The XSD should be something like this:

<xs:schema xmlns="www.test.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="www.test.com">
  <xs:complexType name="TEST_CLASS">
    <xs:sequence>
      <xs:element name="TEST">
        <xs:complexType>
          <xs:attribute name="TYPE"/>
          <xs:attribute name="DURATION"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

第二个问题是XmlSchema 对象应该使用XmlSchema.Read() 方法加载:

The second problem is that XmlSchema objects should be loaded using theXmlSchema.Read() method:

using (XmlReader reader = XmlReader.Create(xsdDir + xsdFile)) {
  XmlSchema schema = XmlSchema.Read(reader, null); 
  . . . 
}

这篇关于在 WCF 中使用 XmlSerializer 序列化 IXmlSerializable 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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