0

I'm very new c# and xml

I'm trying to convert a txt file to XML, then with a button apply an XSLT to the XML with C# (and display it in the program)

I have managed to convert the txt file to XML (although it only takes 1 lot of data), but cannot figure out how to apply the XSLT to the XML.

Is there some way to insert the stylesheet link into the XML () or is this what "XSLcompiledTransform" is meant to do ?

Below is the c# that I have been testing -

    private void button1_Click(object sender, EventArgs e)
    {
        string filePath = "authors.xml";

        dataSet1.ReadXml(filePath);

        dataGridView1.DataSource = dataSet1;
        dataGridView1.DataMember = "authors";
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        String[] data = File.ReadAllLines("TextFile.txt");

        XElement authors =
            new XElement("authors",
             new XElement("title", data[0]),
             new XElement("Released", data[1]),
             new XElement("Price", data[2]));



        authors.Save("authors.Xml");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        try
        {

            // Generating a temporary HTML file
            string outfile = Path.GetTempFileName().Replace(".tmp", ".html");

            // Creating the XslCompiledTransform object
            XslCompiledTransform transform = new XslCompiledTransform();

            // Loading the stylesheet file from the textbox
            transform.Load("authors.xslt");

            // Transforming the XML file and storing output in HTML file
            transform.Transform("authors.xml", outfile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        // Create a reader that contains the style sheet.
        XmlReader reader = XmlReader.Create("authors.xslt");
        reader.ReadToDescendant("xsl:stylesheet");

        // Load the style sheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(reader);

    }

    private void button5_Click(object sender, EventArgs e)
    {
        string filePath = "authors.xml";

        string[] lines = File.ReadAllLines(filePath);

        textBox1.Text = System.IO.File.ReadAllText(filePath);
    }
}
}
luapbbor
  • 1
  • 1
  • Maybe the following link can help you: http://www.codeproject.com/Articles/87621/Introduction-to-XML-and-XSLT-in-Csharp-Net – Sabrina_cs Mar 05 '16 at 19:41
  • I have already tried the solution for the question marked as duplicate and it works fine when saving to html, however when saving to xml, it does not. – luapbbor Mar 06 '16 at 18:53
  • Is there some way to just insert the link to the xslt stylesheet into the xml ? – luapbbor Mar 06 '16 at 19:08

0 Answers0