0

I am looking for a way to compare the entire contents of a specified text file to a certain branch of an XML file's contents, and have the console output either a 1 or 0 depending on if the two match. The part of the xml I would like to compare looks like:

-<root>-<Info><Seperator>DATA TO BE COMPARED</Seperator></Info>

The XML has more data below it, but I only want to compare this line to the text file. eg. if the xml file contained "test" in the column, and all the text file had written in it was "test" it would output a 1, but it the xml said "test" and the text file said "test123" it would not.

The closest thing I have found to this is: Xml Comparison in C# to compare two xml files, and http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/59db1bb1-d822-4db7-b09c-f7d2d5c705b4 to compare two txt file.

Community
  • 1
  • 1
JosephG
  • 3,111
  • 6
  • 33
  • 56
  • Should this be tagged as homework? – David Hoerster May 19 '12 at 22:12
  • Nope, i'm trying to do this for a program i'm writing, but am new to C# @DavidHoerster Thanks for any help you can provide – JosephG May 19 '12 at 22:24
  • Does the file only contain a single line? – yamen May 19 '12 at 22:53
  • Which part of program you have problem with? You don't know how to read from text file? Or how to work with XML? Or how to compare texts? Your question is too extensive to be answered entirely. – Ňuf May 19 '12 at 22:57
  • Sorry, I should have been more clear, I was having an issue reading the XML data of only a specific part of the file. But the answer below helps! @Nuf – JosephG May 20 '12 at 16:40

1 Answers1

0

Ignoring the fact that Seperator is spelt wrong, and assuming there's a single Seperator node in the XML, you can use LINQ to XML to read the relevant node:

var xml = "<root><Info><Seperator>DATA TO BE COMPARED</Seperator></Info></root>";   
var xmlString = XElement.Parse(xml).Descendants("Seperator").FirstOrDefault().Value;

And then you can use File.ReadAllText to read the file as a string:

var fileString = File.ReadAllText(@"c:\test.txt");

Then finally the comparison:

Console.WriteLine("{0}", fileString == xmlString ? 1 : 0);
yamen
  • 15,390
  • 3
  • 42
  • 52
  • Thank you! that's perfect, sorry for the simple question, this is my first week trying to teach myself C#, generally in the past I have used python and Java. – JosephG May 20 '12 at 16:37