Reading flat file and generating tagged file

This is a discussion on Reading flat file and generating tagged file within the C# Programming forums, part of the General Programming Boards category; Hi, I'm a c# newbie and wish to read an i/p flat file of the form: A 1 2 10 ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    Reading flat file and generating tagged file

    Hi,

    I'm a c# newbie and wish to read an i/p flat file of the form:



    A 1 2 10

    B 4 10 0

    C 10 3 0

    C B A


    After reading it I wish to generate a tagged file fo the format:

    <SET>
    <TAG tagID = 'A'>
    .
    .
    </TAG>
    </SET>

    <SUB>
    <SB sID = 'A' eID = 'B'/>
    .
    .

    </SUB>

    Now the SUB tagis filled up only if the cell-value in the read item is non-zero...i.e. there will be no entry as sID= B eID = A & sID = C and eID = A...

    Could anyone please give me clues, urls on how to approach this?
    Will XMLWriter be of anyhelp?

    Appreciate your guidance,
    Angkar

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I can't give you any specific examples right now (away from compiler and all), but XMLTextWriter is a good first step. Look into WriteStartDocument, WriteStartElement, WriteStartAttribute, WriteEndDocument, WriteEndElement, and WriteEndAttribute.
    Last edited by pianorain; 03-22-2006 at 09:42 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Here's an example:
    Code:
    class Program
    {
    	static void Main(string[] args)
    	{
    		using (XmlTextWriter xmlWriter = new XmlTextWriter("test.xml", Encoding.Unicode))
    		{
    			xmlWriter.WriteStartDocument();
    			//Make sure to enclose all tags in one top-level tag
    			xmlWriter.WriteStartElement("Body");
    
    			xmlWriter.WriteStartElement("Tag1");
    			xmlWriter.WriteAttributeString("Attr1", "AttrValue1");
    			xmlWriter.WriteAttributeString("Attr2", "AttrValue2");
    			xmlWriter.WriteValue("Value1");
    			xmlWriter.WriteEndElement(); //</Tag1>
    
    			xmlWriter.WriteStartElement("Tag2");
    			xmlWriter.WriteAttributeString("Attr1", "AttrValue1");
    			xmlWriter.WriteAttributeString("Attr2", "AttrValue2");
    			xmlWriter.WriteValue("Value2");
    			xmlWriter.WriteEndElement(); //</Tag2>
    
    			xmlWriter.WriteEndElement(); //</Body>
    			xmlWriter.WriteEndDocument();
    		}
    	}
    }
    Output:
    Code:
    <?xml version="1.0" encoding="utf-16"?>
    <Body>
    <Tag1 Attr1="AttrValue1" Attr2="AttrValue2">Value1</Tag1>
    <Tag2 Attr1="AttrValue1" Attr2="AttrValue2">Value2</Tag2>
    </Body>
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thank you so much. But you didn't mention how to read the file and store the values.
    Appreciate your help.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    To read a file, just use a StreamReader. How you read the data from the string returned by the Read methods depends on the data. The following example reads a comma-delimited file of numbers:
    Code:
    class Program
    {
    	static void Main(string[] args)
    	{
    		string[] elements;
    		List<List<int>> data = new List<List<int>>();
    		using (StreamReader reader = new StreamReader("test.csv"))
    		{
    			do
    			{
    				elements = reader.ReadLine().Split(',');
    				data.Add(new List<int>());
    				foreach (string element in elements)
    				{
    					if(element.Length > 0)
    						data[data.Count - 1].Add(int.Parse(element));
    				}
    			} while (!reader.EndOfStream);
    		}
    
    		foreach(List<int> list in data)
    		{
    			foreach (int item in list)
    			{
    				if (item < 10)
    				{
    					Console.Write(string.Format(" {0} ", item.ToString()));
    				}
    				else
    					Console.Write(string.Format("{0} ", item.ToString()));
    			}
    
    			Console.WriteLine();
    		}
    
    		Console.ReadLine();
    	}
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 02:24 PM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 01:45 PM
  3. Generating data file names from program variables
    By a380x in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 12:12 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21