Kim KangIn
Kim KangIn

Reputation: 349

XML Schema for analysis in C#

Is it possible to use a XML Schema to check against the contents of a XML file?

For instance, in ASP.NET web.config, can I create a schema to check that <customErrors mode = "On">? This will ultimately be used in a C# app, the C# app should take in the XML document and the XML Schema and check if the XML Document violates any of the "rules" listed in the XML schema, i.e. <customErrors mode = "Off">

Is it possible to do the checking without any boundary to the structure of the XML file? i.e. the attribute <customErrors> can be within any part of the XML document and the schema will still work.

Upvotes: 1

Views: 117

Answers (1)

kjhughes
kjhughes

Reputation: 111785

Possible: Yes, in XML Schema 1.1 using assertions.

Practical or recommended: No.

XML Schema is intended to be used to validate the "structure of the XML file," as you anticipate in your question. You can skip much of that via xsd:any and then use assertions to express the sort of spot-checks that you describe via XPath expressions. However, it'd be more natural to just apply XPath expressions directly to your XML from within C#, or using Schematron, which is a standard for applying XPath expressions to do validation.

Upvotes: 1

Related Questions