using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace AdvancementVoyage.Magic.Utility
{
///
/// A helper class that provides common XML reading functionality.
///
internal static class GenericXmlReaderHelper
{
///
/// Validates data against the supplied schema and returns the desired
/// collection of objects.
///
/// The type of object being populated.
/// The function responsible for translating the XML into objects.
/// The schema that the XML stream will be validated against.
/// The actual xml data.
/// The collection of objects from the xml string.
public static IList ReadTargetCollection(Func> parserFunc, StringReader schema, StringReader data)
{
var sc = XmlSchema.Read(schema, HandleValidationError);
var settings = new XmlReaderSettings();
settings.Schemas.Add(sc);
settings.ValidationType = ValidationType.Schema;
using (var reader = XmlReader.Create(data))
{
return parserFunc(reader);
}
}
///
/// The method that executes on a failed XML validation.
///
///
/// The source of the event.
///
///
/// Arguments describing the cause of the failure.
///
private static void HandleValidationError(object src, ValidationEventArgs args)
{
Trace.Fail(string.Format("Invalid data format: {0}", args.Message));
}
}
}