简体   繁体   English

在具有XmlTypeAttribute的生成类上实现IXmlSerializable

[英]Implementing IXmlSerializable on a generated class that has XmlTypeAttribute

Basically, the initial problem is I need to make a boolean value serialize as 0 or 1. The solution I found was to implement IXmlSerializable, which I did. 基本上,最初的问题是我需要将一个布尔值序列化为0或1。我发现的解决方案是实现IXmlSerializable,我这样做了。 Unfortunately the class I'm trying to serialize is generated code off a schema and has an XmlTypeAttribute on it. 不幸的是,我要序列化的类是根据架构生成的代码,并且具有XmlTypeAttribute。 When I try to (de)serialize the object with the XmlSerializer created in the usual manner ( new XmlSerializer(type) ) it throws this exception: 当我尝试使用以通常方式创建的XmlSerializer( new XmlSerializer(type) )对对象进行反序列化时,它将引发以下异常:

System.InvalidOperationException: Only XmlRoot attribute may be specified for the type ______ Please use XmlSchemaProviderAttribute to specify schema type.

Two options come to mind immediatly: 立即想到两个选择:

1) remove the attribute in the generated code. 1)在生成的代码中删除属性。 This change would have to be made every time the code was re-generated. 每次重新生成代码时都必须进行此更改。

2) Use an XmlAttributeOverrides object when creating the serializer to remove the attribute. 2)创建序列化程序以删除属性时,请使用XmlAttributeOverrides对象。 This would require the rest of the code base to "know" that it needs to override that attribute. 这将要求其余代码库“知道”它需要覆盖该属性。 Also, the exception thrown gives absolutly no clue as to what needs to be done to fix it. 而且,抛出的异常绝对无法提供解决方案。

Both options kinda stink. 两种选择都有些臭。 Is there a third option? 还有第三种选择吗?

I have the same problem, for me removing the IXMLSerializable works, I don't use it, and have you tried to hide the true or false with a some logic in the properties? 我有同样的问题,对于我删除IXMLSerializable的作品,我不使用它,并且您是否尝试过使用属性中的某些逻辑隐藏true或false? Like this: 像这样:

private bool mblnFlag;

public String Flag 
{
   get
   {
      return mblnFlag;
   }
   set
   {
      mblnFlag = (value == "1")
   }
}

Of course you should enhance the properties and do more checking, but that's the idea. 当然,您应该增强属性并进行更多检查,但这就是想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在集合对象上实现IXmlSerializable - Implementing IXmlSerializable on a collection object 实现IXmlSerializable的WCF词典 - WCF Dictionary implementing IXmlSerializable XmlTypeAttribute仅适用于类中的属性 - XmlTypeAttribute works only on attributes in class 为包含集合的类实现IXmlSerializable - Implementing IXmlSerializable for Classes Containing Collections 在基类中实现IXmlSerializable时如何恢复到“默认”XML序列化? - How to revert back to 'default' XML serialization when implementing IXmlSerializable in a base class? 带有IXmlSerializable的自定义类因OutOfMemoryException而失败 - Custom class with IXmlSerializable fails with OutOfMemoryException 反序列化实现 IXmlSerializable 的类型集合永远运行 - Deserializing collection of types implementing IXmlSerializable runs forever IXmlSerializable抽象类创建异常 - IXmlSerializable abstract class creation exception 是否可以在类XMLTypeAttribute中具有两个名称空间来处理2个肥皂响应的反序列化? - Is that possible to have two namespaces in the class XMLTypeAttribute to handle deserialization of 2 soap responses? 为包含或不带有CDATA标签的数据的内容实现IXmlSerializable - Implementing IXmlSerializable for content containing data with or without CDATA tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM