view Xml/XmlResourceResolver.cs @ 114:7ca4acc659bb

* Add resource resolver for use with DTDs as resources * Add helper method for loading schemas into a set from resources
author IBBoard <dev@ibboard.co.uk>
date Mon, 25 Jun 2012 21:06:25 +0100
parents
children 07660ac09a5f
line wrap: on
line source

using System;
using System.Xml;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace IBBoard.Xml
{
		public class XmlResourceResolver : XmlUrlResolver
		{
				private Assembly assm;
				private Dictionary<string, string> relativeToUriMap = new Dictionary<string, string>();

				public XmlResourceResolver(Assembly assembly)
				{
						assm = assembly;
				}

				public void AddMapping(string relativeURI, string resourceName)
				{
						relativeToUriMap[relativeURI] = resourceName;
				}

				public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
				{
						string absoluteUriString = absoluteUri.ToString();

						if (relativeToUriMap.ContainsKey(absoluteUriString))
						{
								string file = relativeToUriMap[absoluteUriString];
								Stream stream = assm.GetManifestResourceStream(file);
								return stream;
						}

						return base.GetEntity(absoluteUri, role, ofObjectToReturn);
				}
		}
}