You should show us what code you already have, then ask how to approach the part that you cannot solve.
I suggest you to store a list of headers, and a list or dictionary of rows. This way you could check if a header already exists.
For example:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace TestElements
{
public class Elements
{
public static string[] ROW_SEPARATOR = { " " };
public static string[] ELEMENT_SEPARATOR = { "|" };
private int _nextRowId;
public List<string> ColumnHeaders;
public Dictionary<int, Dictionary<string, string>> Rows;
public Elements()
{
this._nextRowId = 0;
this.ColumnHeaders = new List<string>();
this.Rows = new Dictionary<int, Dictionary<string, string>>();
}
public void AddFromFile(string path)
{
// Read all the file, and split in lines
string[] lines = File.ReadAllText(path).Split(ROW_SEPARATOR, StringSplitOptions.None);
// Get the headers
List<string> headers = lines[0].Split(ELEMENT_SEPARATOR, StringSplitOptions.None).ToList();
// Add headers that are new
foreach (string header in headers)
{
if (!this.ColumnHeaders.Contains(header))
{
this.ColumnHeaders.Add(header);
}
}
// Parse every line
for (int i = 1; i < lines.Length; i++)
{
// Split a line into elements
List<string> elements = lines[i].Split(ELEMENT_SEPARATOR, StringSplitOptions.None).ToList();
// Build a row of elements
Dictionary<string, string> row = new Dictionary<string,string>();
for (int j = 0; j < headers.Count; j++)
{
row.Add(headers[j], elements[j]);
}
// Add the row to our store
this.AddRow(row);
}
}
private void AddRow(Dictionary<string, string> rowdata)
{
this.Rows.Add(this._nextRowId, rowdata);
this._nextRowId++;
}
}
}
Take care I'm not checking for errors in this example.
Then you can build the output text as you wants.
If you have any more concrete question, or this does not help enough, just ask.
EDIT: Here is an example of usage
// Create a few files to use in a test
string TextFile1 =
"Element1|Element2|Element4|Element5| " +
"00000001|00000002|00000004|00000005| " +
"00000011|00000012|00000014|00000015| " +
"00000021|00000022|00000024|00000025| " +
"00000031|00000032|00000034|00000035|";
string TextFile2 =
"Element2|Element3|Element4|Element5| " +
"00000002|00000003|00000004|00000005| " +
"00000012|00000013|00000014|00000015| " +
"00000022|00000023|00000024|00000025|";
string TextFile3 =
"Element1|Element3|Element4|Element6| " +
"00000001|00000003|00000004|00000006| " +
"00000011|00000013|00000014|00000016| " +
"00000021|00000023|00000024|00000026| " +
"00000031|00000033|00000034|00000036| " +
"00000041|00000042|00000044|00000045|";
File.WriteAllText("File1.txt", TextFile1);
File.WriteAllText("File2.txt", TextFile2);
File.WriteAllText("File3.txt", TextFile3);
// Read the files into our class
Elements elements = new Elements();
elements.AddFromFile("File1.txt");
elements.AddFromFile("File2.txt");
elements.AddFromFile("File3.txt");
// Build the result
StringBuilder sb = new StringBuilder();
// First build headers
foreach (string header in elements.ColumnHeaders)
{
sb.Append(header);
sb.Append("|");
}
sb.Append(Environment.NewLine);
// Next add every row
foreach (Dictionary<string, string> row in elements.Rows.Values)
{
foreach (string header in elements.ColumnHeaders)
{
if (row.ContainsKey(header))
{
sb.Append(row[header]);
}
else
{
sb.Append("________");
}
sb.Append("|");
}
sb.Append(Environment.NewLine);
}
// Finally save the result into a file
File.WriteAllText("Result.txt", sb.ToString());
And the result looks like this:
Element1|Element2|Element4|Element5|Element3|Element6|
00000001|00000002|00000004|00000005|________|________|
00000011|00000012|00000014|00000015|________|________|
00000021|00000022|00000024|00000025|________|________|
00000031|00000032|00000034|00000035|________|________|
________|00000002|00000004|00000005|00000003|________|
________|00000012|00000014|00000015|00000013|________|
________|00000022|00000024|00000025|00000023|________|
00000001|________|00000004|________|00000003|00000006|
00000011|________|00000014|________|00000013|00000016|
00000021|________|00000024|________|00000023|00000026|
00000031|________|00000034|________|00000033|00000036|
00000041|________|00000044|________|00000042|00000045|