3

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. Unfortunately the class I'm trying to serialize is generated code off a schema and has an XmlTypeAttribute on it. When I try to (de)serialize the object with the XmlSerializer created in the usual manner ( new XmlSerializer(type)) it throws this exception:

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. 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. 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?

Josh Sterling
  • 838
  • 7
  • 12
  • 1
    _why_ does it have the `[XmlType]` attribute? – John Saunders Apr 28 '10 at 05:56
  • That's just the way Xsd2Code generated it. Saying the schema I'm using is a massive beast is an understatement. Xsd.exe and other tools either crashed or spat out 40mb of code using custom libraries. For other reasons I ended up having to modify the generated code anyway. However, if there is a third way around this issue it might work for the other one as well. – Josh Sterling Apr 28 '10 at 17:40

1 Answers1

0

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? 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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I got around my issue by commenting out the XmlTypeAttribute, however modifying the generating code feels dirty and i was hoping there was a third option that didn't involve modifying generated code. I'm probably just dreaming :( – Josh Sterling May 03 '10 at 13:16