this was part of my module in a project to generate xml files from prefix expression. this might be use ful to some one else as well.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
ArrayList arrlist = new ArrayList();
arrlist.Add("+a***-bcdsq");
arrlist.Add("+a+/-bc*de/fg");
arrlist.Add("+a*-bc-de");
arrlist.Add("+a-bc");
arrlist.Add("+*+ab-cd/+ef-gh");
arrlist.Add("+/+ab*cde");
arrlist.Add("-*21/*34+57");
arrlist.Add("+-*21/*3457");
string xmlString = string.Empty;
int i = 0;
string path = Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
Console.WriteLine(path);
foreach (string s in arrlist)
{
Console.WriteLine("Exp : " + s);
StreamWriter sw = new StreamWriter(path.Replace(@"file:\","") + @"\File" + i.ToString() + ".xml");
int q = 0;
sw.Write(generateXML1(s,1,ref q));
sw.Flush();
sw.Close();
sw.Dispose();
i++;
}
}
public static string generateXML1(string expression, int values, ref int charProc)
{
string xmlString = string.Empty;
int Operatorcount = 0;
int characterProcessed = 0;
for (int i = 0; i < expression.Length && Operatorcount < values; i++)
{
char c = expression[i];
switch (c)
{
case '+':
xmlString += "<Add>";
xmlString += generateXML1(expression.Substring(i + 1), 2, ref characterProcessed);
i += characterProcessed;
xmlString += "</Add>";
break;
case '-':
xmlString += "<Sub>";
xmlString += generateXML1(expression.Substring(i + 1), 2, ref characterProcessed);
i += characterProcessed;
xmlString += "</Sub>";
break;
case '/':
xmlString += "<Div>";
xmlString += generateXML1(expression.Substring(i + 1), 2, ref characterProcessed);
i += characterProcessed;
xmlString += "</Div>";
break;
case '*':
xmlString += "<Mul>";
xmlString += generateXML1(expression.Substring(i + 1), 2, ref characterProcessed);
i += characterProcessed;
xmlString += "</Mul>";
break;
default:
xmlString += "<Value>" + c.ToString() + "</Value>";
break;
}
Operatorcount++;
characterProcessed++;
}
charProc = charProc + characterProcessed;
return xmlString;
}
}
}