Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

generate xml file for prefix expressions

5.00/5 (1 vote)
9 Mar 2010CPOL 1  
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{ ...
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.

C#
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();
            //(2*1)-(3*4)/(5+7)     -*21/*34+57
            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"); //     ((2*1)-((3*4)/5))+7
            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++;
            }

            //Console.ReadLine();
        }


        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;
        }


    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)