mkmurray
mkmurray

Reputation: 2444

Return correct data type from XML element with xsi:type attribute specified

Does the .NET framework have a built-in API for parsing some of the primitive data types of XML Schema and turning them into appropriate .NET data types? For example:

<someElement 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:type="xs:dateTime">2013-03-01T18:36:00Z</someElement>

I'd like to write some C# code that would key off of the xsi:type attribute and give me the equivalent .NET data type, ideally with some built-in API from .NET. I have just recently found the XmlConvert class with it's several ToXXX() static methods (like ToDateTime(), but I'd want it to pick the appropriate type automatically.

Important caveat: I'd strongly prefer if possible to avoid using the xsd.exe tool that auto generates C# class declarations based on an XML schema file.

Upvotes: 1

Views: 892

Answers (1)

MiMo
MiMo

Reputation: 11953

No, there is nothing that does what you want automatically in .NET.

The closest thing is the code generated automatically by the objecte serializer/deserializers - that can actually read and use xsi:type to generate objects of different classes when de-serializing a XML element, but it does not really work for primitive types liked dates (and is quite cumbersome to use - you have to declare classes and de-serialize them).

Upvotes: 1

Related Questions