无法将按钮归因于XSLT转换(C#)

时间:2015-11-18 15:51:51

标签: c# xml xslt transform

我正在尝试创建一个接口,允许我选择3个XSLT文件并将它们合并在一起,然后使用XML进行转换。我有一个工作转换代码,我正在为。

创建一个用户界面

转换代码:

public static void Transform(string sXmlPath, string sXslPathBody, string sXslPathHead, string sXslPathFoot, string sXslPathMerged)
        {
            try
            {
                XNamespace ns = "http://www.w3.org/1999/XSL/Transform";

                //load Xml
                XPathDocument myXPathDoc = new XPathDocument(sXmlPath);

                //Load Body
                XElement xslt = XElement.Load(sXslPathBody);

                //Add Code To Body
                xslt.AddFirst(new XElement(ns + "include", new XAttribute("href", sXslPathFoot)));
                xslt.AddFirst(new XElement(ns + "include", new XAttribute("href", sXslPathHead)));

                XElement body = xslt.Descendants("body").Single();

                body.AddFirst(new XElement(ns + "call-template", new XAttribute("name", "Header")));
                body.Add(new XElement(ns + "call-template", new XAttribute("name", "Footer")));

                //Save Combined File
                //XElement.Save("c:\temp.xlst");
                xslt.Save(sXslPathMerged);

                XslCompiledTransform myXslTrans = new XslCompiledTransform();

                //load Combined File 
                myXslTrans.Load(sXslPathMerged);

                //Merge XML with Combined File
                XmlTextWriter myWriter = new XmlTextWriter ("result.html", null);

                //transform Xml
                myXslTrans.Transform(myXPathDoc, null, myWriter);

                myWriter.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
        }

转换代码工作正常,但我试图将每个阶段设置为一个按钮,这将允许我选择我希望的任何3个XSLT文件,以及我希望的任何XML。然后是一个按钮,将3个XSLT文件合并在一起创建一个HTML输出。

代码:

public partial class Form1 : Form
    {
        String HeadFileSelected, BodyFileSelected, FootFileSelected, XmlFileSelected, MergeFiles;

        public Form1()
        {
            InitializeComponent();
        }

        //Head
        private void openFileHead_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void header_Click(object sender, System.EventArgs e)
        {
            if (openFileHead.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                HeadFileSelected = openFileHead.FileName;

                string filename = HeadFileSelected;

                textBox2.Text = HeadFileSelected;
            }
        }

        //Body
        private void openFileBody_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void body_Click(object sender, System.EventArgs e)
        {
            if (openFileBody.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BodyFileSelected = openFileBody.FileName;

                string filename = BodyFileSelected;

                textBox3.Text = BodyFileSelected;
            }
        }

        //Foot
        private void openFileFooter_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void footer_Click(object sender, System.EventArgs e)
        {
            if (openFileFooter.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FootFileSelected = openFileFooter.FileName;

                string filename = FootFileSelected;

                textBox4.Text = FootFileSelected;
            }
        }

        //Xml
        private void openFileXml_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void xml_Click(object sender, System.EventArgs e)
        {
            if (openFileXml.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                XmlFileSelected = openFileXml.FileName;
            }
        }

        //Merge
        private void openFileMerge_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void merge_Click(object sender, EventArgs e)
        {

            //XmlTransformUtil objXMLTrans = new XmlTransformUtil();
            XmlTransformUtil.Transform(XmlFileSelected, BodyFileSelected, HeadFileSelected, FootFileSelected, MergeFiles);
        }
    }

我已成功获得XML选择& 3个XSLT选项工作正常它只是合并按钮无效,因为我无法将String MergeFiles;归因于Transform(string sXslPathMerged)

我理解为什么它不起作用,但我现在不能提供能够得到我需要的结果的解决方案。

1 个答案:

答案 0 :(得分:0)

管理要弄清楚,我需要将MergeFiles定义为已保存的XSLT,因为它是null,所以它不会去任何地方。是通过在我的表单中添加以下内容来实现的。

MergeFiles = "C:\\Users\\user\\Desktop\\TEMP.xslt";