Beispiel #1
0
        /// <summary>
        /// GetXmlFromTemplate
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="filename"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public string GetXmlFromTemplate(Assembly assembly, string filename, string[] args = null)
        {
            string result = string.Empty;

            try
            {
                XmlTemplate xmltemplate = new XmlTemplate();
                xmltemplate.LoadFromResource(assembly, filename);
                xmltemplate.Execute(args);

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xmltemplate.InstanceAsXmlString());
                if (xmlDocument.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
                {
                    xmlDocument.RemoveChild(xmlDocument.FirstChild);
                }

                result = xmlDocument.OuterXml;
            }
            catch (SystemException contextualException)
            {
                Console.WriteLine("Unable to obtain {0}", filename);
                if (!contextualException.Message.Contains("Error loading XML template from resource"))
                {
                    throw contextualException;
                }
            }
            return(result);
        }
Beispiel #2
0
 /// <summary>
 /// Constructs a temporary file in the current directory for the current instance of an XmlTemplate.
 /// The file will have UTF-8 encoding and the extension "xml".
 /// </summary>
 /// <param name="xmlTemplate">An XmlTemplate containing the XML content to be used for the temporary file.</param>
 public TempFile(XmlTemplate xmlTemplate)
 {
     try
     {
         _directory = Directory.GetCurrentDirectory();
         _name      = Guid.NewGuid().ToString();
         _extension = "xml";
         _filePath  = Path.Combine(_directory, _name + "." + _extension);
         _encoding  = Encoding.UTF8;
         WriteContent(xmlTemplate.InstanceAsXmlString());
     }
     catch (Exception exception)
     {
         //ContextualException contextualException = new ContextualException( "Error creating temporary file " + _filePath + ".", exception );
         //contextualException.EventType = EventLogEntryType.Error;
         //contextualException.EventId = 405;
         //ExceptionManager.HandleException( contextualException, PolicyName.SystemException );
         throw exception;
     }
 }