Updated XmlHelper class to use the XDocument class on .NET 4.0 and XNA 4.0 builds

git-svn-id: file:///srv/devel/repo-conversion/nusu@205 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2010-09-19 22:37:09 +00:00
parent 6e9c296e6d
commit 8c63901338
6 changed files with 68 additions and 10 deletions

View File

@ -18,7 +18,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\net-2.0\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;UNITTEST</DefineConstants>
<DefineConstants>TRACE;DEBUG;UNITTEST;USE_XMLDOCUMENT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\net-2.0\Debug\Nuclex.Support.xml</DocumentationFile>
@ -27,7 +27,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\net-2.0\Release\</OutputPath>
<DefineConstants>TRACE;UNITTEST</DefineConstants>
<DefineConstants>TRACE;UNITTEST;USE_XMLDOCUMENT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\net-2.0\Release\Nuclex.Support.xml</DocumentationFile>

View File

@ -18,7 +18,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\xna-3.1-xbox360\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;XBOX;XBOX360;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3</DefineConstants>
<DefineConstants>TRACE;DEBUG;XBOX;XBOX360;USE_XMLDOCUMENT;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>
@ -30,7 +30,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\xna-3.1-xbox360\Release\</OutputPath>
<DefineConstants>TRACE;XBOX;XBOX360;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3</DefineConstants>
<DefineConstants>TRACE;XBOX;XBOX360;USE_XMLDOCUMENT;NO_SERIALIZATION;NO_SYSTEMEVENTS;XNA_3</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>

View File

@ -22,7 +22,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\xna-4.0-phone7\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<DefineConstants>TRACE;DEBUG;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>
@ -34,7 +34,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\xna-4.0-phone7\Release\</OutputPath>
<DefineConstants>TRACE;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<DefineConstants>TRACE;WINDOWS_PHONE;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>

View File

@ -22,7 +22,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\xna-4.0-xbox360\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<DefineConstants>TRACE;DEBUG;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>
@ -34,7 +34,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\xna-4.0-xbox360\Release\</OutputPath>
<DefineConstants>TRACE;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLDOCUMENT;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<DefineConstants>TRACE;XBOX;XBOX360;NO_CLONING;NO_SERIALIZATION;NO_XMLSCHEMA;NO_SYSTEMEVENTS;NO_EXITCONTEXT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoStdLib>true</NoStdLib>

View File

@ -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
}
}
}

View File

@ -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 {
/// <summary>Helper routines for handling XML code</summary>
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
/// <summary>Loads an XML document from a file</summary>
/// <param name="schema">Schema to use for validating the XML document</param>
/// <param name="documentPath">
@ -179,6 +184,52 @@ namespace Nuclex.Support {
}
}
#else // !USE_XMLDOCUMENT
/// <summary>Loads an XML document from a file</summary>
/// <param name="schema">Schema to use for validating the XML document</param>
/// <param name="documentPath">
/// Path to the file containing the XML document that will be loaded
/// </param>
/// <returns>The loaded XML document</returns>
public static XDocument LoadDocument(XmlSchema schema, string documentPath) {
using(FileStream documentStream = openFileForSharedReading(documentPath)) {
return LoadDocument(schema, documentStream);
}
}
/// <summary>Loads an XML document from a stream</summary>
/// <param name="schema">Schema to use for validating the XML document</param>
/// <param name="documentStream">
/// Stream from which the XML document will be read
/// </param>
/// <returns>The loaded XML document</returns>
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
/// <summary>Opens a file for shared reading</summary>
/// <param name="path">Path to the file that will be opened</param>
/// <returns>The opened file's stream</returns>
@ -209,7 +260,7 @@ namespace Nuclex.Support {
return false;
}
#endif // !NO_XMLDOCUMENT
#endif // !NO_XMLSCHEMA
}