繁体   English   中英

我如何检查textbox.text是否等于我的第一行file.xml

[英]how can i check if the textbox.text is equal to my first line file.xml

我想知道如何检查用户类型的textbox1.text是否等于我的login.xml文件第一行的文本,以及if第二个textbox.text是否等于我的登录的第二行.xml文件,如果是,他可以登录,如果没有显示messabox说passwor或用户名不正确。 我会贬低那个。

顺便说一句,这是login.xml文件中的内容

<?xml version="1.0" encoding="UTF-8"?>

<informacao xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Data1>master</Data1> //this is the first line i want to make this the username
  <Data2>master</Data2> //this is the second line i want to make this the password
</informacao>

您可以使用Linq to Xml从xml文件中读取值:

var xdoc = XDocument.Load("login.xml"); // load xml file into document
var userName = (string)xdoc.Root.Element("Data1"); // get value of Data1 element
var password = (string)xdoc.Root.Element("Data2");

if (textbox1.Text != userName || textbox2.Text != password)
{
    MessageBox.Show("Username and/or password is invalid");
    return;
}

注意:对xml元素和UI控件使用更好的命名。 将用户凭证存储在xml文件中也不是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM