0

I need to create a program (preferably in C#) that reads in 2 files of different types (most likely .txt and .xls) with slightly different fields and compares each line. For example: File 1 (.txt): Last name: Smith First name: Bill EID: bs223 Type: Accounting File 2 (.xls): last: smith first: bill EID: bs223 number: 5555555 type: accounting

The files have some same fields, some different. Also, the text file will have many spaces in between fields. I need to compare common fields and display to the user the similarities and differences. The format of the fields in each file will be consistent.

My question is, what would be the best option to take in doing this? Would it be wise to convert both files to XML and compare through that? How would XML come into the equation if i used it? Are there better methods in C#?

Thanks!

EDIT: The code to convert .txt to XML

public static void Main() {
  XElement root = new XElement("root");
  foreach (String ln in File.ReadAllLines(@"input.txt")){
            string[] fields = ln.Split(' '); 
            XElement record = new XElement("record");
            int pos = 0;
            foreach (String sp in fields){
              if(sp != ""){
                pos += 1;
                XElement field = new XElement(string.Format("field_{0}", 
                pos.ToString()));
                field.Add(sp);
                record.Add(field);
                         }
                 }
            root.Add(record); 
            }
            Console.Write (root.ToString());
          }
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
Cassus
  • 199
  • 6
  • 14

1 Answers1

0

How frequently you need to do this exercise? If this is one time activity, I would suggest you can read both files in the format they are in and just perform a string comparison. While doing comparison make sure to ignore spaces, commas and casing.

Have a look at this one if more keen on xml - How would you compare two XML Documents?, but that internally does the same thing and I doubt would give u any performance improvement. And also you need to include your efforts to convert both of them to xml.

Community
  • 1
  • 1
JustCode
  • 312
  • 1
  • 3
  • It would be used multiple times (once a week or so) for about a year. I can convert the text file into XML, however, I am just confused on if the method I have in mind is a decent one: Convert both text and .xls to xml, and compare like fields? – Cassus Jun 23 '13 at 01:13