Пример #1
0
        static XmlDoc Load(string fileName, string cachePath, bool allowRedirect)
        {
            LoggingService.Debug("Loading XmlDoc for " + fileName);
            XmlDoc doc;
            string cacheName = null;

            if (cachePath != null)
            {
                Directory.CreateDirectory(cachePath);
                cacheName = cachePath + "/" + Path.GetFileNameWithoutExtension(fileName)
                            + "." + fileName.GetHashCode().ToString("x") + ".dat";
                if (File.Exists(cacheName))
                {
                    doc = new XmlDoc();
                    if (doc.LoadFromBinary(cacheName, File.GetLastWriteTimeUtc(fileName)))
                    {
                        //LoggingService.Debug("XmlDoc: Load from cache successful");
                        return(doc);
                    }
                    else
                    {
                        doc.Dispose();
                        try {
                            File.Delete(cacheName);
                        } catch {}
                    }
                }
            }

            try {
                using (XmlTextReader xmlReader = new XmlTextReader(fileName)) {
                    xmlReader.MoveToContent();
                    if (allowRedirect && !string.IsNullOrEmpty(xmlReader.GetAttribute("redirect")))
                    {
                        string redirectionTarget = GetRedirectionTarget(xmlReader.GetAttribute("redirect"));
                        if (redirectionTarget != null)
                        {
                            LoggingService.Info("XmlDoc " + fileName + " is redirecting to " + redirectionTarget);
                            return(Load(redirectionTarget, cachePath, false));
                        }
                        else
                        {
                            LoggingService.Warn("XmlDoc " + fileName + " is redirecting to " + xmlReader.GetAttribute("redirect") + ", but that file was not found.");
                            return(new XmlDoc());
                        }
                    }
                    doc = Load(xmlReader);
                }
            } catch (XmlException ex) {
                LoggingService.Warn("Error loading XmlDoc " + fileName, ex);
                return(new XmlDoc());
            }

            if (cachePath != null && doc.xmlDescription.Count > cacheLength * 2)
            {
                LoggingService.Debug("XmlDoc: Creating cache for " + fileName);
                DateTime date = File.GetLastWriteTimeUtc(fileName);
                try {
                    doc.Save(cacheName, date);
                } catch (Exception ex) {
                    LoggingService.Error("Cannot write to cache file " + cacheName, ex);
                    return(doc);
                }
                doc.Dispose();
                doc = new XmlDoc();
                doc.LoadFromBinary(cacheName, date);
            }
            return(doc);
        }
Пример #2
0
 public virtual void Dispose()
 {
     xmlDoc.Dispose();
     isDisposed = true;
 }