Last active
October 12, 2020 17:08
-
-
Save matlus/df7a5904e8c2e92c907534fdf49b4bfc to your computer and use it in GitHub Desktop.
SYWTBACR - Xml Schema Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal static class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| /* | |
| * We have an XM Document and now we're attempting to validate the schema of | |
| * this document against an Xml Schema document. The next line of code is | |
| * simply attempting to retrieve the Xml Schema document. | |
| * The applicationRootFolder argument is determined at runtime (dependant upon the runtime context; Console app or Web App etc.) | |
| * The fileName argument comes from the config file. The concatenation of the 2 is the full path and file name | |
| * The arguments to the method below have been hard coded for the sake brevity | |
| */ | |
| var schema = GetSchemaDocumentFromFile(@"..\..\", "XMLSchema1.xsd"); | |
| /* | |
| * Now that we have a schema document, we can validate our XmlDocument against the schema | |
| * This part of the code has been intentionally elided | |
| */ | |
| } | |
| private static XmlSchema GetSchemaDocumentFromFile(string applicationRootFolder, string fileName) | |
| { | |
| using (var stream = File.OpenRead(applicationRootFolder + fileName)) | |
| { | |
| XmlSchema schema = null; | |
| try | |
| { | |
| schema = XmlSchema.Read(stream, null); | |
| } | |
| catch (Exception e) | |
| { | |
| throw new MyApplicatioNameSchemaException(e.Message); | |
| } | |
| AddDataElementToSchemaIfNotPresent(schema); | |
| return schema; | |
| } | |
| } | |
| private static void AddDataElementToSchemaIfNotPresent(XmlSchema schema) | |
| { | |
| //// The implementation of this method has been intentionally elided | |
| } | |
| } | |
| [Serializable] | |
| public class MyApplicatioNameSchemaException : Exception | |
| { | |
| public MyApplicatioNameSchemaException() { } | |
| public MyApplicatioNameSchemaException(string message) : base(message) { } | |
| public MyApplicatioNameSchemaException(string message, Exception inner) : base(message, inner) { } | |
| protected MyApplicatioNameSchemaException( | |
| System.Runtime.Serialization.SerializationInfo info, | |
| System.Runtime.Serialization.StreamingContext context) : base(info, context) { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment