Kasbolat Kumakhov
Kasbolat Kumakhov

Reputation: 721

Typesafe XML parsing and validation

I have different XML file types which look something like this:

XML_A:

NODE_1
NODE_2

XML_B:

NODE_1
NODE_2
NODE_3

XML_C:

NODE_2
NODE_3

XML_D:

NODE_1
NODE_3

And also corresponding XSD schemes per each type and generated C# classes from them.

Now imaging i need to validate these nodes and compare their values with a database and with eachother (some kind of relationship validation).

Each node has it's own validator and it's same in all XML files (e.g. NODE_1 has same meaning everywhere, just like NODE_2, etc.).

Currently i validate everything with something like this:

public bool ValidateNode_1(XElement rootElement)
{
    var node_1 = rootElement.XPathSelectElement("....");
    //validation logic here
}

Since some node types are duplicated (e.g. NODE_1 in XML_A, XML_B, XML_D) there is one validator per node type.

The problem comes when the XMLs are updated (new version with nodes removed or changed their parent-child relationships). It's not happening by surprise and i get all the new schemes and classes so i can prepare for that. But validation is text based, so i need to update all the validators manually, adjusting node names and XPaths (and there are 100+ validators).

What i want is to use the generated C# classes for validation, so simple refactoring would work with renaming and compile time errors for changed relationships. But since there are multiple types of XML files i can't just do like this:

public bool ValidateNode_1(NodeClass nodeClass)
{
    var node_1 = nodeClass.NodeValue;
    //validation logic here
}

Because there are multiple NodeClass classes defined for each XML type, even though they are identical. They have no relation to eachother and reside in separate C# namespaces. Although namespace for XML and XSD is same.

So the question is, how to use generated C# classes for this kind of validation and not duplicate the validators per node type?

P.S. XSD version is 1.0.

Upvotes: 0

Views: 114

Answers (0)

Related Questions