Create XML Files without using HTML Tags
page 3 of 4
by Andrew Mooney
Feedback
Average Rating: 
Views (Total / Last 10 Days): 25072/ 82

Creating the Files

[ Download Code ]
Three files (XML, XSL, HTML) are created using the System.IO.StreamWriter class. To create the XML file the string arrays Fields1 and Types1 are iterated. The Fields1 strings become elements and the Types1 strings are used to add some initial data that of the specified type. You will also notice that two rows of data are added to the XML file instead of just one. The reason is that if an XML file has at least two rows it can be edited using Microsoft Excel without creating a schema file. I will be covering this topic in my next article. The XSL and HTML files are fairly straight forward but, I will say that it is easier to run the application and the examine these files after they are created to examine their contents.

Here is the code for creating these three files:

// Create XML file
StreamWriter StreamWriter1 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + 
File1 + ".xml");
StreamWriter1.WriteLine("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r"); 
StreamWriter1.WriteLine("<!-- Created using XmlCreator 1.0 -->\r");
StreamWriter1.WriteLine("<dataroot>\r");
for(int x = 0; x < 2; x++)
{
 StreamWriter1.WriteLine("<" + File1 + ">\r");
 for(int a = 0; a < Number1; a++)
 {
 if(Types1[a].ToUpper() == "N")
 {
  StreamWriter1.WriteLine("<" + Fields1[a] + ">123.45</" + Fields1[a] + ">\r");
 }
 else if(Types1[a].ToUpper() == "D")
 {
  StreamWriter1.WriteLine("<" + Fields1[a] + ">1/1/2005</" + Fields1[a] + ">\r");
 }
 else if(Types1[a].ToUpper() == "B")
 {
  StreamWriter1.WriteLine("<" + Fields1[a] + ">true</" + Fields1[a] + ">\r");
 }
 else
 {
  StreamWriter1.WriteLine("<" + Fields1[a] + ">String Data</" + Fields1[a] + ">\r");
 }
 }
 StreamWriter1.WriteLine("</" + File1 + ">\r");
}
StreamWriter1.WriteLine("</dataroot>\r");
StreamWriter1.Close();
// Create XSL file
StreamWriter StreamWriter2 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + 
File1 + ".xsl");
StreamWriter2.WriteLine("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r"); 
StreamWriter2.WriteLine("<xsl:stylesheet version=\"1.0\" 
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\r");
StreamWriter2.WriteLine("<!-- Created using XmlCreator 1.0 -->\r");
StreamWriter2.WriteLine("<xsl:template match=\"/\">\r");
StreamWriter2.WriteLine("<html>\r");
StreamWriter2.WriteLine("<head>\r");
StreamWriter2.WriteLine("<title>\r");
StreamWriter2.WriteLine(File1 + "\r");
StreamWriter2.WriteLine("</title>\r");
StreamWriter2.WriteLine("<style type=\"text/css\">\r");
StreamWriter2.WriteLine("body{font-Family:Arial,Helvetica;}\r");
StreamWriter2.WriteLine("table, td, 
th{border-color:navy;border-style:solid;border-width:1PX;}\r");
StreamWriter2.WriteLine("</style>\r");
StreamWriter2.WriteLine("</head>\r");
StreamWriter2.WriteLine("<body>\r");
StreamWriter2.WriteLine("<table cellspacing=\"0\" cellpadding=\"2\">\r");
StreamWriter2.WriteLine("<caption><b>" + File1 + "</b></caption>\r");
StreamWriter2.WriteLine("<tr>\r");
for(int y = 0; y < Number1; y++)
{
 StreamWriter2.WriteLine("<th>" + Fields1[y] + "</th>\r");
}
StreamWriter2.WriteLine("</tr>\r");
StreamWriter2.WriteLine("<xsl:for-each select=\"dataroot/" + 
File1 + "\">\r");
StreamWriter2.WriteLine("<tr>\r");
for(int b = 0; b < Number1; b++)
{
 StreamWriter2.WriteLine("<td><xsl:value-of select=\"" + Fields1[b] +
"\"/></td>\r");
}
StreamWriter2.WriteLine("</tr>\r");
StreamWriter2.WriteLine("</xsl:for-each>\r");
StreamWriter2.WriteLine("</table>\r");
StreamWriter2.WriteLine("</body>\r");
StreamWriter2.WriteLine("</html>\r");
StreamWriter2.WriteLine("</xsl:template>\r");
StreamWriter2.WriteLine("</xsl:stylesheet>\r");
StreamWriter2.Close();
// Create HTML file
StreamWriter StreamWriter3 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + 
File1 + ".htm");
StreamWriter3.WriteLine("<html>\r");
StreamWriter3.WriteLine("<!-- Created using XmlCreator 1.0 -->\r");
StreamWriter3.WriteLine("<body>\r");
StreamWriter3.WriteLine("<script type=\"text/javascript\">\r");
StreamWriter3.WriteLine("var Xml1 = new ActiveXObject(\"Microsoft.XMLDOM\")\r");
StreamWriter3.WriteLine("Xml1.async = false\r");
StreamWriter3.WriteLine("Xml1.load(\"" + File1 + ".xml\")\r");
StreamWriter3.WriteLine("var Xsl1 = new ActiveXObject(\"Microsoft.XMLDOM\")\r");
StreamWriter3.WriteLine("Xsl1.async = false\r");
StreamWriter3.WriteLine("Xsl1.load(\"" + File1 + ".xsl\")\r");
StreamWriter3.WriteLine("document.write(Xml1.transformNode(Xsl1))\r");
StreamWriter3.WriteLine("</script>\r");
StreamWriter3.WriteLine("</body>\r");
StreamWriter3.WriteLine("</html>\r");
StreamWriter3.Close();


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-19 3:46:03 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search