private static PageTemplateConfiguration LoadPageTemplateConfiguration(string configurationPath) { PageTemplateConfiguration rtnConfig = null; XmlSerializer serializer = new XmlSerializer(typeof(PageTemplateConfiguration)); bool pageAssemblyInstructionXmlValid = true; pageAssemblyInstructionXmlValid = PageAssemblyInstructionFactory.ValidateXml(PageTemplateConfigurationPath, HttpContext.Current.Server.MapPath(ContentDeliveryEngineConfig.PageAssembly.PageAssemblyInfoTypes.XsdPath)); try { using (FileStream xmlFile = File.Open(configurationPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) { rtnConfig = (PageTemplateConfiguration)serializer.Deserialize(XmlReader.Create(xmlFile)); } } catch (Exception ex) { //TODO: Do something here } return(rtnConfig); }
/// <summary> /// Creates an IPageAssemblyInstruction derived instance from the XML at the specified path. /// /// /// </summary> public static IPageAssemblyInstruction GetPageAssemblyInfo(string requestedPath) { bool pageAssemblyInstructionXmlValid = true; string xmlFilePath = HttpContext.Current.Server.MapPath(String.Format(ContentDeliveryEngineConfig.PathInformation.PagePathFormat.Path, requestedPath)); // Input validation. if (xmlFilePath == null) { throw new ArgumentNullException("pathToXml", "The pathToXml parameter cannot be null."); } if (!File.Exists(xmlFilePath)) { return(null); } if (ContentDeliveryEngineConfig.PageAssembly.PageAssemblyInfoTypes.EnableValidation == true) { pageAssemblyInstructionXmlValid = PageAssemblyInstructionFactory.ValidateXml(xmlFilePath, HttpContext.Current.Server.MapPath(ContentDeliveryEngineConfig.PageAssembly.PageAssemblyInfoTypes.XsdPath)); } if (pageAssemblyInstructionXmlValid == false) { return(null); } // Load the XML file into an XmlReader and create a IPageAssemblyInstruction derived instance from the XML. IPageAssemblyInstruction pageAssemblyInfo = null; try { using (FileStream xmlFile = File.Open(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) { using (XmlReader xmlReader = XmlReader.Create(xmlFile)) { // Read just enough to get the page type of assembly info object we want to create // e.g. a SinglePageAssemblyInfo, BookletAssemblyInfo, etc. xmlReader.MoveToContent(); // Get the name of hte serializer type we need (the document element name is the // name of the type). string pageAssemblyInfoTypeName = xmlReader.LocalName; // Make sure the serializer we need is in the cache (if it isn't, we've got invalid XML). if (Instance._serializers.ContainsKey(pageAssemblyInfoTypeName) == false) { string message = String.Format("Unable to find XmlSerializer for type \"{0}.\" Either \"{0}\" is an unsupported type and the XML file at \"{1}\" is invalid or the page assembly configuration needs a new entry to support \"{0}\"", pageAssemblyInfoTypeName, xmlFilePath); throw new PageAssemblyException(message); } // Get the serializer from the cache. XmlSerializer serializer = Instance._serializers[pageAssemblyInfoTypeName]; // Deserialize the XML into an object. pageAssemblyInfo = (IPageAssemblyInstruction)serializer.Deserialize(xmlReader); } } } catch (Exception ex) { throw new PageAssemblyException(String.Format("Unable to create IPageAssemblyInfo for file \"{0}.\"", xmlFilePath), ex); } return(pageAssemblyInfo); }