Xml schema validation

This transformation allows you to validate XML code in your incoming data against a XML schema definition.


To use the XmlSchemaValidation you need to define how the XML string can be read from your data row and the schema definition used for validation. If the schema is not valid, the complete row will be send to the error output of the transformation.


The XmlSchemaValidation is part of the ETLBox.Xml package


The XmlSchemaValidation is a non-blocking transformation and has an input buffer.


Define the schema in your code:

string xsdMarkup = @"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
   <xsd:element name='Root'>
            <xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
            <xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>

Now you can define a POCO use to store your xml (but of course you can also use a dynamic object).

public class MyXmlRow
  public string Xml { get; set; }

Now we setup some test data:

string _validXml => $@"<?xml version=""1.0"" encoding=""utf-8""?>

string _invalidXml => $@"<?xml version=""1.0"" encoding=""utf-8""?>

Finally, we can create a flow that validates the incoming xml agains our schema:

var source = new MemorySource<MyXmlRow>();
source.DataAsList.Add(new MyXmlRow() { Xml = _validXml });
source.DataAsList.Add(new MyXmlRow() { Xml = _invalidXml });
MemoryDestination<MyXmlRow> dest = new MemoryDestination<MyXmlRow>();
MemoryDestination<ETLBoxError> error = new MemoryDestination<ETLBoxError>();

XmlSchemaValidation<MyXmlRow> schemaValidation = 
  new XmlSchemaValidation<MyXmlRow>();
schemaValidation.XmlSelector = r => r.Xml;
schemaValidation.XmlSchema = xsdMarkup;