我有一個應用程序將xml文件與文本文件進行比較,如果xml的特定行的內容與文本文件相同,則程序輸出1,但是如果它們是不同的,它輸出0.1 Unfortunatley我無法得到它的輸出1.它似乎總是輸出0「If」和「else」C#不能正確輸出
代碼:
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");
}
}
}
}
}
}
}
XML文件(該程序應該只讀取數據部分):
<?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>
和文本文件只包含:
1
當我做Console.WriteLine(fileString);
或
Console.WriteLine(xmlString);
他們做輸出同樣的事情,不過,像他們應該,這讓我困惑,甚至更多。
您是否附加了調試器來查看? – leppie