您不應該以純XML存儲密碼;然而,我認爲這是你要找的那種比較:
var userSought = "userA";
var passwordSought = "1234";
XDocument xml = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Users",
new XElement("User",
new XElement("Username", "Admin"),
new XElement("Password", "123")),
new XElement("User",
new XElement("Username", "userA"),
new XElement("Password", "123"))
));
var userMatch = (from userElement in xml.Element("Users").Elements("User")
where userElement.Element("Username").Value == userSought
select new
{
Username = userElement.Element("Username").Value,
Password = userElement.Element("Password").Value
}).FirstOrDefault();
if(userMatch.Username == userSought && userMatch.Password == passwordSought)
Console.WriteLine(userSought + " matches password " + passwordSought);
else
Console.WriteLine(userSought + " has password " + userMatch.Password + " but is expected to be " + passwordSought);
你不應該在XML文件中存儲明文密碼。如果你不想處理一個真正的數據庫,你應該看看類似SQLite的東西。 – Guvante
我正在使用MD5哈希計劃我不會將密碼存儲爲純文本,但這不是問題,因爲我已經想通了。 – Marc
這是非常簡單的事情,谷歌將解決 –