當前位置: 首頁>>代碼示例>>C#>>正文


C# XslCompiledTransform.TransformFileToFile方法代碼示例

本文整理匯總了C#中System.Xml.Xsl.XslCompiledTransform.TransformFileToFile方法的典型用法代碼示例。如果您正苦於以下問題:C# XslCompiledTransform.TransformFileToFile方法的具體用法?C# XslCompiledTransform.TransformFileToFile怎麽用?C# XslCompiledTransform.TransformFileToFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Xml.Xsl.XslCompiledTransform的用法示例。


在下文中一共展示了XslCompiledTransform.TransformFileToFile方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

        static void Main(string[] args)
        {

            if ((args.Length < 2) || (args.Length > 2))
            {
                Console.WriteLine("Syntax:\r\n\r\nsitegenerator source-path target-path\r\n");
                return;
            }

            string sourceDirectory = args[0];
            string websiteDirectory = args[1];

            if (!Directory.Exists(sourceDirectory))
            {
                Console.WriteLine("Error: Source path not found\n\n");
            }

            if (!Directory.Exists(websiteDirectory))
            {
                Console.WriteLine("Error: Target path not found\n\n");
            }

            Console.WriteLine("Source: {0}\nTarget: {1}", sourceDirectory, websiteDirectory);

            string sourceConfigDirectory = Path.Combine(sourceDirectory,"config");
            string sourcePageDirectory = Path.Combine(sourceDirectory,"pages");

            Helpers.CopyDirectoryFiles(Path.Combine(sourceDirectory, "css"), Path.Combine(websiteDirectory, "css"),true);
            Helpers.CopyDirectoryFiles(Path.Combine(sourceDirectory, "js"), Path.Combine(websiteDirectory, "js"),true);
            Helpers.CopyDirectoryFiles(Path.Combine(sourceDirectory, "root"), websiteDirectory,false);

            const string DATAFILENAME = "data.xml";

            // Load style sheets
            XslCompiledTransform xsltArticle = new XslCompiledTransform();

            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.DtdProcessing = DtdProcessing.Parse;

            XmlReader reader = XmlReader.Create(Path.Combine(sourceConfigDirectory, "data.xslt"), readerSettings);

            XsltSettings xsltSettings = new XsltSettings();
            xsltSettings.EnableDocumentFunction = true;
            xsltSettings.EnableScript = true;

            xsltArticle.Load(reader, xsltSettings, new XmlUrlResolver());

            XslCompiledTransform xsltNavigation = new XslCompiledTransform();
            xsltNavigation.Load(Path.Combine(sourceConfigDirectory, "navigation.xslt"));

            string navigationHtml = "";

            try
            {
                var articles = Directory.EnumerateDirectories(sourcePageDirectory);

                foreach (string currentDir in articles)
                {
                    Console.WriteLine("Processing {0}", currentDir);

                    string currentArticle = Path.GetFileName(currentDir);

                    string sourceArticleDirectory = Path.Combine(sourcePageDirectory, currentArticle);
                    string websiteArticleDirectory = Path.Combine(websiteDirectory, "pages", currentArticle);

                    DirectoryInfo di = Directory.CreateDirectory(websiteArticleDirectory);

                    string infilename = Path.Combine(sourceArticleDirectory, DATAFILENAME);

                    int numberOfPages = 1;


                    XmlDocument xmlfile = new XmlDocument();
                    xmlfile.Load(infilename);
                    numberOfPages = xmlfile.SelectNodes("//page").Count;
                    
                    XsltArgumentList argsListNavigation = new XsltArgumentList();
                    argsListNavigation.AddParam("selectedArticleName", "", currentArticle);

                    navigationHtml = xsltNavigation.TransformFileToString(Path.Combine(sourceConfigDirectory, "navigation.xml"), argsListNavigation);


                    for (int currentPageno = 1; currentPageno < (numberOfPages + 1); currentPageno++)
                    {
                        string outfilename;
                        outfilename = Path.Combine(websiteArticleDirectory, "page_" + currentPageno.ToString() + ".html");

                        XsltArgumentList argsListData = new XsltArgumentList();
                        argsListData.AddParam("selectedPageNo", "", currentPageno.ToString());
                        argsListData.AddParam("navigationContent", "", navigationHtml);

                        xsltArticle.TransformFileToFile(infilename, argsListData, outfilename);
                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
//.........這裏部分代碼省略.........
開發者ID:nocoolnicksleft,項目名稱:SiteGenerator,代碼行數:101,代碼來源:Program.cs


注:本文中的System.Xml.Xsl.XslCompiledTransform.TransformFileToFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。