V
V
Valery Abakumov2015-09-21 17:57:23
.NET
Valery Abakumov, 2015-09-21 17:57:23

C# - What's the best way to work with XSD schemas?

Good day!
First I will outline the situation, and then I will describe the question itself.
There is an application that sends xml-messages somewhere (let it be "to the Server"). XML messages sent to the Server must conform to XSD schemas. These schemes are supplied from the outside, and N-applications from M developers work on them. It is clear that schemes can vary greatly from version to version. The main condition is that my application must support the current version of XSD schemas and 1 previous one. Now at the code level it is implemented as follows. There is a class that describes the XSD schema itself (tags in the form of properties). When sending a message to the Server, as a developer, I initialize an object of the required class (read, "XSD schemas"), fill in all the necessary properties of this class (read, "XML tags") and call the Send method. This method, using reflection from my class, forms an XML string (yes, yes, exactly an object of type String), which is then transferred as bytes to another application. This code is inherited.
Attention, question, dear colleagues!
What is the best way to organize the code in such a way that there is less hassle when changing the version of XSD schemas? How do you form messages according to XSD schemas?
I have several options, I will try to describe their pros and cons.
1. Leave as it is now (when the XSD schema is described by a C# type). Manually change this class each time the XSD schema changes. And since you need to support 2 versions (current and previous), it turns out that you need to create 2 versions of these classes. In this case, one of them will be inherited from the other (for example, the MyXsdMessageCurrentVersion class describes the XSD schema. So, I need to make 2 classes: MyXsdMessageCurrentVersion - for the current version, and MyXsdMessagePreviousVersion - for the previous one, and one of them is inherited from the other. Which one from which I don't even know yet...).
Pros : higher compilation speed (compared to option number 2), clarity for the developer (we work with specific objects).
Minuses: 1) developer's time for "manual" generation of C# classes, 2) "crutches" in the form of if....else ("if ... something ..., then we use the current version, otherwise - the previous one). Considering if there are a lot of messages, then there will be a lot of "crutches" if...else... Somehow I don't really like it...
2. Rewrite the mechanism in such a way that it generates C# objects based on XSD using reflection -schemas (so far I have a bad idea of ​​the implementation, because I haven’t thought much about it.)
It seems to me that it has pluses : 1) you don’t bother with C# classes, class inheritance to maintain 2 versions of XSD schemas
.: 1) the application from a dumb one will become completely dumb, because reflection + reflection is in large numbers, because, let me remind you, messages are sent every second and every minute, and 2) significant resource costs for rewriting this mechanism (it is very voluminous and several almost the same applications).
What do you think is the best way to solve the problem in order to comply with the conditions, but at the same time reduce the number of "crutches" and implement the mechanism in such a way that the number of changes made is minimized?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mrrl, 2015-09-21
@Mrl

There is a program xsd.exe that generates C# classes from an xsd file. You can run it before building the project - it will do everything. True, what to do with two schemes is not very clear (read one, and if it gave an error, then the other?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question