Create XML Files without using HTML Tags
Published: 13 Jan 2005
Unedited - Community Contributed
Some XML editors will automatically add the closing tag after you have finished typing the opening tag but, you still have to type the brackets around the opening tag. I kept thinking this process should be easier. So, I came up with a solution that allows you to create XML files without using HTML tags.
by Andrew Mooney
Average Rating: 
Views (Total / Last 10 Days): 25665/ 83


[ Download Code ]

Creating XML files with a text editor would be a lot easier if you didn’t have to close all those HTML tags. First you have to add the XML declaration and the root opening and closing HTML tags. Next, you start adding element opening and closing tags one at a time. Of course, once you have the initial sequence completed you can just copy and paste to repeat the required elements. After doing this hundreds of times you’ll be looking for a faster way to create XML files.

Some XML editors will automatically add the closing tag after you have finished typing the opening tag but, you still have to type the brackets around the opening tag. I kept thinking this process should be easier. So, I came up with a solution that allows you to create XML files without using HTML tags.

This console application will create an XML file based on user input. Just enter the file name, how many element fields you want, and the name of each field. Optionally, you can include a data type separated by a comma after the field name. You can just enter the field name because the data type is not required. The structure of the XML file that is created will be compatible with the .NET DataSet and can be easily added to a database.

In addition to creating the XML file, an XSL file and HTML file are also created. The HTML file uses client side JavaScript to transform the XML file using the XSL file. This provides an easy way to view the new XML file by displaying it in a table layout.

The download includes both the source code and the already compiled application. You can start using the executable right away or customize it to meet your needs. All you will need is the .NET Framework and a text editor, like Notepad, to build this application.

The information that is usually found in the AsemblyInfo.cs file has been added to the XmlCreator.cs file so that everything needed to compile the application is in a single file.

using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("XmlCreator")]
[assembly: AssemblyDescription("Easily Create Xml Files")]
[assembly: AssemblyCompany("Andrew Mooney")]
[assembly: AssemblyProduct("XmlCreator")]
[assembly: AssemblyCopyright("Copyright (c) 2004 by Andrew Mooney")]
[assembly: AssemblyVersion("")]

Collecting User Input

[ Download Code ]
The first thing we need to do is define variables that will hold the user input. The string File1 will hold the file name, the string Count1 holds the number of element fields that will be added to the XML file, and the integer Number1 will hold the converted Count1 value. Two string arrays Fields1 and Types1 will contain element field names and data types. All string and string arrays are initialized as empty strings.

A while loop is used when capturing input to prevent the input of empty strings. The file name should be entered without the extension (.xml). When the number of fields is entered an integer data type check is made. If conversion to an integer fails the string Count1 is set to empty and the loop continues to ask for input.

When capturing each field name a check is made to see if the optional data type is used. The format for this is (field name, data type). Acceptable data types are numeric (N), date (D), boolean (B), and string (S). If left blank the string data type is used as default. 

Example Use:
Please enter the file name:NewFile
Please enter the number of fields:3
Please enter the name for field 1: Id,N
Please enter the name for field 2: DatePublished,D
Please enter the name for field 3: Title

This code is used to collect user input:

// Collect user input
string File1 = "";
string Count1 = "";
int Number1 = 0;
string[] Fields1;
string[] Types1;
while(File1 == String.Empty)
 Console.Write("Please enter the file name:") ;
 File1 = Console.ReadLine();
while(Count1 == String.Empty)
 Console.Write("Please enter the number of fields:") ;
 Count1 = Console.ReadLine();
  Number1 = Convert.ToInt32(Count1);
  Count1 = "";
Fields1 = new string[Number1];
Types1 = new string[Number1];
for(int i = 0; i < Number1; i++)
 int Field = i + 1;
 Fields1[i] = "";
 Types1[i] = "";
 while(Fields1[i] == String.Empty)
  Console.Write("Please enter the name for field " 
  + Field.ToString() + " :") ;
  Fields1[i] = Console.ReadLine();
  if(Fields1[i].IndexOf(",") > 0)
   string[] Field1 = new string[2];
   char[] Char1= {','};
   Field1 = Fields1[i].Split(Char1);
   Fields1[i] = Field1[0];
   Types1[i] = Field1[1].ToUpper();

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");
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");
  StreamWriter1.WriteLine("<" + Fields1[a] + ">String Data</" + Fields1[a] + ">\r");
 StreamWriter1.WriteLine("</" + File1 + ">\r");
// 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\" 
StreamWriter2.WriteLine("<!-- Created using XmlCreator 1.0 -->\r");
StreamWriter2.WriteLine("<xsl:template match=\"/\">\r");
StreamWriter2.WriteLine(File1 + "\r");
StreamWriter2.WriteLine("<style type=\"text/css\">\r");
StreamWriter2.WriteLine("table, td, 
StreamWriter2.WriteLine("<table cellspacing=\"0\" cellpadding=\"2\">\r");
StreamWriter2.WriteLine("<caption><b>" + File1 + "</b></caption>\r");
for(int y = 0; y < Number1; y++)
 StreamWriter2.WriteLine("<th>" + Fields1[y] + "</th>\r");
StreamWriter2.WriteLine("<xsl:for-each select=\"dataroot/" + 
File1 + "\">\r");
for(int b = 0; b < Number1; b++)
 StreamWriter2.WriteLine("<td><xsl:value-of select=\"" + Fields1[b] +
// Create HTML file
StreamWriter StreamWriter3 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + 
File1 + ".htm");
StreamWriter3.WriteLine("<!-- Created using XmlCreator 1.0 -->\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");


[ Download Code ]
After the application is finished a final message is displayed stating that three files have been created. These files are created in the same directory where the console application is located. Then the program waits for one final input. This allows the user to see the file creation message. Just press enter to end the program or close the window.

// Final message
Console.Write("The following files have been created: " + File1 + ".xml, " + 
File1 + ".xsl, " + File1 + ".htm. ") ;
// Keeps the application running until enter is pressed
string input = Console.ReadLine();

Here is what you will see when running the application:

Please enter the file name: Contacts
Please enter the number of fields:3
Please enter the name for field 1: Name
Please enter the name for field 2: Address
Please enter the name for field 3: Phone
The following files have been created: Contacts.xml, Contacts.xsl, Contacts.htm

This application is simple and very useful for web developers by providing a means to quickly create XML files without the tedious use of HTML tags. It would be very easy to customize it for creating specific types of XML and XSL files. I hope you find a place for it in your toolbox as I have.

User Comments

No comments posted yet.

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

©Copyright 1998-2021  |  Page Processed at 2021-12-02 11:03:17 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search