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

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();
 try
 {
  Number1 = Convert.ToInt32(Count1);
 }
 catch
 {
  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();
  }
 }
}


View Entire Article

User Comments

No comments posted yet.






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


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-12-18 5:07:30 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search