Dynamically create a text file from a C# program
- by techstu
Can I dynamically create a text file from a C# program, using data from a previously created xml file and text file, I have written half the code, but can't go any further please help
using System;
using System.IO;
using System.Xml;
namespace Task3
{
   class TextFileReader
   {
      static void Main(string[] args)
      {
         String strn=" ", strsn=String.Empty;  
         XmlTextReader reader = new XmlTextReader("my.xml");
                 while (reader.Read())
                 {
                    switch (reader.NodeType)
                    {
                       case XmlNodeType.Element: // The node is an element.
                          if (reader.HasAttributes)
                          {
                             strn = reader.GetAttribute(0);
                             strsn = reader.GetAttribute(1);
                             int counter = 0;
                             string line;
                             // Read the file and display it line by line.
                             System.IO.StreamReader file = new System.IO.StreamReader("read_file.txt");
                             string ch, ch1;
                             while ((line = file.ReadLine()) != null)
                             {
                                if (line.Substring(0, 1).Equals("%"))
                                {
                                   int a = line.IndexOf('%');
                                   int b = line.LastIndexOf('%');
                                   ch = line.Substring(a + 1, b - 1);
                                   ch1 = line.Substring(a, b+1);
                                   if (ch == "name")
                                   {
                                      string test = line.Replace(ch1, strn);
                                      Console.WriteLine(test);
                                   }
                                   else if (ch == "sirname")
                                   {
                                      string test = line.Replace(ch1, strsn);
                                      Console.WriteLine(test);
                                   }
                                }
                                else
                                {
                                   Console.WriteLine(line);
                                }
                                counter++;
                             }
                             file.Close();
                          }
                          break;
                    }
                 }        
         // Suspend the screen.
         Console.ReadLine();
      }
   }
}
  the xml file from which i am reading
  is:
<?xml version="1.0" encoding="utf-8" ?> 
- <Workflow>
  <User UserName="pqr" Sirname="sbd" /> 
  <User UserName="abc" Sirname="xyz" /> 
  </Workflow>
  and the text file is:
hi this is me
%sirname%
%name%
but this is not wat i want..please help