Monday, March 30, 2015

XSLT compile error.

Thank you Fouad, that's very helpful. I need to output at least two styles to be viewed in a browser.


I am getting another error now:



The program isn't writing to my xml file. Any idea where I am going wrong? Thanks in advance for your help. Here is my code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace CSVImporter
{

public partial class CSVImporter : Form
{
//const string xmlfilename = @"C:\Users\fenwky\XmlDoc.xml"; - file name and location of xml file
const string xmlfilename = @"C:\Users\fenwky\XmlDoc.xml";

// New code
//const string xmlfilename = @"C:\Users\fenwky\XmlDoc.xml"; - file name and location of xsl file
const string stylesheetsimple = @"C:\Users\fenwky\style1.xsl";


//const string xmlfilecomplex = @"C:\Users\fenwky\XmlDoc2.xml";
const string xmlfilecomplex = @"C:\Users\fenwky\XmlDoc2.xml";

DataSet ds = null;


public CSVImporter()
{
InitializeComponent();
// Create a Open File Dialog Object.
openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialog1.ShowDialog();
string fileName = openFileDialog1.FileName;


//doc.InsertBefore(xDeclare, root);
// Create a CSV Reader object.
CSVReader reader = new CSVReader();
ds = reader.ReadCSVFile(fileName, true);
dataGridView1.DataSource = ds.Tables["Table1"];

}

private void WXML_Click(object sender, EventArgs e)
{
WriteXML();
}

public void WriteXML()
{

StringWriter stringWriter = new StringWriter();
ds.WriteXml(new XmlTextWriter(stringWriter), XmlWriteMode.WriteSchema);
string xmlStr = stringWriter.ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
XmlDeclaration xDeclare = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

doc.InsertBefore(xDeclare, doc.FirstChild);

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("style1.xsl");

// Transform the file and output an HTML string.
string HTMLoutput;
StringWriter writer = new StringWriter();
xslt.Transform("XmlDoc.xml", null, writer);
HTMLoutput = writer.ToString();
writer.Close();

var piText = "type=\"text/xsl\" href=\"style1.xsl\"";
var newPI = doc.CreateProcessingInstruction("xml-stylesheet", piText);
doc.InsertAfter(newPI, doc.FirstChild);

// Save document
doc.Save(xmlfilename);

}

private void btExportComplexXML_Click(object sender, EventArgs e)
{
WriteXMLComplex();
}

public void WriteXMLComplex()
{
// Creates stringwriter
StringWriter stringWriter = new StringWriter();
ds.WriteXml(new XmlTextWriter(stringWriter), XmlWriteMode.WriteSchema);

string xmlStr = stringWriter.ToString();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);

XmlDeclaration xDeclare = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

doc.InsertBefore(xDeclare, doc.FirstChild);

// Create a procesing instruction.
// XmlProcessingInstruction newPI;
// Uses XML transformation.
// String PItext = "<abc:stylesheet xmlns:abc=\"http://ift.tt/1N6W08z; version=\"1.0\">";
// newPI = doc.CreateProcessingInstruction("xsl:stylesheet", PItext);
// doc.InsertAfter(newPI, doc.FirstChild);

// Try this code as
var piText = "type=\"text/xsl\" href=\"style1.xsl\"";
var newPI = doc.CreateProcessingInstruction("xml-stylesheet", piText);
doc.InsertAfter(newPI, doc.FirstChild);

// Saves document.
doc.Save(xmlfilecomplex);

}
}

//Creates a CSVReader Class
public class CSVReader
{

public DataSet ReadCSVFile(string fullPath, bool headerRow)
{

string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
DataSet ds = new DataSet();

try
{
if (File.Exists(fullPath))
{
string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No");
string SQL = string.Format("SELECT * FROM {0}", filename);
OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr);
adapter.Fill(ds, "TextFile");
ds.Tables[0].TableName = "Table1";
}
foreach (DataColumn col in ds.Tables["Table1"].Columns)
{
col.ColumnName = col.ColumnName.Replace(" ", "_");
}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return ds;
}
}
}


No comments:

Post a Comment