I have an application that compared an xml file to a text file, and if the contents of a specific line of the xml is the same as the text file, the program outputs a 1, but if they are different, it outputs a 0. Unfortunatley I can’t get it to output 1. It always seems to output a 0.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace CompareIt
{
class Program
{
static void Main(string[] args)
{
using (XmlReader reader = XmlReader.Create("2.xml"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.ToLower() == "data")
{
string xml = reader.ReadOuterXml();
var xmlString = (from data in XDocument.Parse(xml).Elements()
select data.Elements().First().Value).FirstOrDefault();
xmlString = xmlString.Replace("\n", "").Trim();
var fileString = File.ReadAllText(@"8.txt");
if (xmlString == fileString)
Console.WriteLine("1");
else
Console.WriteLine("0");
}
}
}
}
}
}
}
The XML file (the program is supposed to only read the data section):
<?xml version="1.0"?>
<root>
<Data>
<Seperator>1</Seperator>
</Data>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>GHOSJRUqcHnZ3M090/5/KhvghyQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>oib+LAXqJshDFm3YM63qkSsJxxF+t0uahGax8tLrjSPJUjW045iYvB4LJCgMeF9oxatbWnVB9hGbvtVnl4iewJDL3kjnjvot5CLozMOaIGJgdys5MP8ncx771itANTm8wi8KgnqVjGjvTakEmcdwcSdRXuCP1WGOwuXm5StkY8Q=</SignatureValue>
</Signature>
</root>
and the text file simply contains a:
1
when I do Console.WriteLine(fileString);
or
Console.WriteLine(xmlString);
they do output the same thing though, like they are supposed to, which confuses me even more.
I have tested your code and it works fine, the only reason may be that your text file contains new line after 1. You may try remove the new line just before the if statement. Use