How can I force this circuit in .NET?

Let's say I have a circuit with which I want the input document to match. I upload the file according to the diagram as follows:

// Load the ABC XSD var schemata = new XmlSchemaSet(); string abcSchema = FooResources.AbcTemplate; using (var reader = new StringReader(abcSchema)) using (var schemaReader = XmlReader.Create(reader)) { schemata.Add(string.Empty, schemaReader); } // Load the ABC file itself var settings = new XmlReaderSettings { CheckCharacters = true, CloseInput = false, ConformanceLevel = ConformanceLevel.Document, IgnoreComments = true, Schemas = schemata, ValidationType = ValidationType.Schema, ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings }; XDocument inputDoc; try { using (var docReader = XmlReader.Create(configurationFile, settings)) { inputDoc = XDocument.Load(docReader); } } catch (XmlSchemaException xsdViolation) { throw new InvalidDataException(".abc file format constraint violated.", xsdViolation); } 

This works great when detecting trivial errors in a file. However, since the schema is locked for the namespace, a document like the one below is invalid, but sneaks through:

 <badDoc xmlns="http://Foo/Bar/Bax"> This is not a valid document; but Schema doesn't catch it because of that xmlns in the badDoc element. </badDoc> 

I would like to say that only namespaces for which I have schemas should pass schema validation.

+8
c # schema xmlreader
source share
3 answers

Oddly enough, the thing you want to see is actually on the XmlReaderSettings object:

 settings.ValidationEventHandler += (node, e) => Console.WriteLine("Bad node: {0}", node); 
+2
source share

The solution I ended up with is basically to verify that the root of the node is in the namespace that I expect. If this is not the case, then I view this in the same way that I consider a scheme authentication failure:

 // Parse the bits we need out of that file var rootNode = inputDoc.Root; if (!rootNode.Name.NamespaceName.Equals(string.Empty, StringComparison.Ordinal)) { throw new InvalidDataException(".abc file format namespace did not match."); } 
+1
source share

All Articles