diff --git a/Nuclex.Support (net-2.0).csproj b/Nuclex.Support (net-2.0).csproj index 7c280cc..fca6981 100644 --- a/Nuclex.Support (net-2.0).csproj +++ b/Nuclex.Support (net-2.0).csproj @@ -18,7 +18,7 @@ full false bin\net-2.0\Debug\ - TRACE;DEBUG;UNITTEST + TRACE;DEBUG;UNITTEST;USE_XMLDOCUMENT prompt 4 bin\net-2.0\Debug\Nuclex.Support.xml @@ -27,7 +27,7 @@ pdbonly true bin\net-2.0\Release\ - TRACE;UNITTEST + TRACE;UNITTEST;USE_XMLDOCUMENT prompt 4 bin\net-2.0\Release\Nuclex.Support.xml diff --git a/Nuclex.Support (xna-3.1-xbox360).csproj b/Nuclex.Support (xna-3.1-xbox360).csproj index 4e387af..a6394af 100644 --- a/Nuclex.Support (xna-3.1-xbox360).csproj +++ b/Nuclex.Support (xna-3.1-xbox360).csproj @@ -18,7 +18,7 @@ full false bin\xna-3.1-xbox360\Debug\ - TRACE;DEBUG;XBOX;XBOX360;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3 + TRACE;DEBUG;XBOX;XBOX360;USE_XMLDOCUMENT;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3 prompt 4 true @@ -30,7 +30,7 @@ pdbonly true bin\xna-3.1-xbox360\Release\ - TRACE;XBOX;XBOX360;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3 + TRACE;XBOX;XBOX360;USE_XMLDOCUMENT;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3 prompt 4 true diff --git a/Nuclex.Support (xna-4.0-phone7).csproj b/Nuclex.Support (xna-4.0-phone7).csproj index ed8e7b6..d2c20cf 100644 --- a/Nuclex.Support (xna-4.0-phone7).csproj +++ b/Nuclex.Support (xna-4.0-phone7).csproj @@ -22,7 +22,7 @@ full false bin\xna-4.0-phone7\Debug\ - TRACE;DEBUG;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT + TRACE;DEBUG;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT prompt 4 true @@ -34,7 +34,7 @@ pdbonly true bin\xna-4.0-phone7\Release\ - TRACE;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT + TRACE;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT prompt 4 true diff --git a/Nuclex.Support (xna-4.0-xbox360).csproj b/Nuclex.Support (xna-4.0-xbox360).csproj index 65601a3..526828d 100644 --- a/Nuclex.Support (xna-4.0-xbox360).csproj +++ b/Nuclex.Support (xna-4.0-xbox360).csproj @@ -22,7 +22,7 @@ full false bin\xna-4.0-xbox360\Debug\ - TRACE;DEBUG;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT + TRACE;DEBUG;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT prompt 4 true @@ -34,7 +34,7 @@ pdbonly true bin\xna-4.0-xbox360\Release\ - TRACE;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT + TRACE;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT prompt 4 true diff --git a/Source/XmlHelper.Test.cs b/Source/XmlHelper.Test.cs index 6207b26..7783007 100644 --- a/Source/XmlHelper.Test.cs +++ b/Source/XmlHelper.Test.cs @@ -24,6 +24,9 @@ using System; using System.IO; using System.Text; using System.Xml; +#if !USE_XMLDOCUMENT +using System.Xml.Linq; +#endif using System.Xml.Schema; using NUnit.Framework; @@ -295,7 +298,11 @@ namespace Nuclex.Support { using( TempFileKeeper tempFile = new TempFileKeeper(conformantXml) ) { +#if USE_XMLDOCUMENT XmlDocument document = XmlHelper.LoadDocument(schema, tempFile); +#else + XDocument document = XmlHelper.LoadDocument(schema, tempFile); +#endif } } } diff --git a/Source/XmlHelper.cs b/Source/XmlHelper.cs index 64e83d6..0825d79 100644 --- a/Source/XmlHelper.cs +++ b/Source/XmlHelper.cs @@ -23,6 +23,9 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Xml; +#if !USE_XMLDOCUMENT +using System.Xml.Linq; +#endif using System.Xml.Schema; namespace Nuclex.Support { @@ -30,7 +33,7 @@ namespace Nuclex.Support { /// Helper routines for handling XML code public static class XmlHelper { -#if !NO_XMLDOCUMENT +#if !NO_XMLSCHEMA #region class ValidationEventProcessor @@ -142,6 +145,8 @@ namespace Nuclex.Support { return false; } +#if USE_XMLDOCUMENT + /// Loads an XML document from a file /// Schema to use for validating the XML document /// @@ -179,6 +184,52 @@ namespace Nuclex.Support { } } +#else // !USE_XMLDOCUMENT + + /// Loads an XML document from a file + /// Schema to use for validating the XML document + /// + /// Path to the file containing the XML document that will be loaded + /// + /// The loaded XML document + public static XDocument LoadDocument(XmlSchema schema, string documentPath) { + using(FileStream documentStream = openFileForSharedReading(documentPath)) { + return LoadDocument(schema, documentStream); + } + } + + /// Loads an XML document from a stream + /// Schema to use for validating the XML document + /// + /// Stream from which the XML document will be read + /// + /// The loaded XML document + public static XDocument LoadDocument(XmlSchema schema, Stream documentStream) { + XmlReaderSettings settings = new XmlReaderSettings(); + settings.Schemas.Add(schema); + + using (XmlReader reader = XmlReader.Create(documentStream, settings)) { + var document = XDocument.Load(reader, LoadOptions.None); + + // Create a schema set because the Validate() method only accepts + // schemas in a schemaset + var schemas = new XmlSchemaSet(); + schemas.Add(schema); + + // Perform the validation and report the first validation error + // encountered to the caller + var validationEventProcessor = new ValidationEventProcessor(); + document.Validate(schemas, validationEventProcessor.OnValidationEvent); + if (validationEventProcessor.OccurredException != null) { + throw validationEventProcessor.OccurredException; + } + + return document; + } + } + +#endif // USE_XMLDOCUMENT + /// Opens a file for shared reading /// Path to the file that will be opened /// The opened file's stream @@ -209,7 +260,7 @@ namespace Nuclex.Support { return false; } -#endif // !NO_XMLDOCUMENT +#endif // !NO_XMLSCHEMA }