C# C XML文件显示登录用户的信息
我正在学习C语言,我正在解决一个问题。我创建了包含不同用户信息的XML文件。我有一个问题,显示一个特定的用户信息时,他的登录。我想显示使用用户名和密码的人的信息 因为现在即使第一个用户登录我也能看到第二个用户的信息 这是我用来显示信息的代码,但它不能正常工作C# C XML文件显示登录用户的信息,c#,xml,winforms,visual-studio,C#,Xml,Winforms,Visual Studio,我正在学习C语言,我正在解决一个问题。我创建了包含不同用户信息的XML文件。我有一个问题,显示一个特定的用户信息时,他的登录。我想显示使用用户名和密码的人的信息 因为现在即使第一个用户登录我也能看到第二个用户的信息 这是我用来显示信息的代码,但它不能正常工作 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("UserInfo.xml"); XmlNodeList nodeList = xmlDoc.DocumentElement.Selec
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo.xml");
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/UsersInfo/UserInfo");
foreach (XmlNode node in nodeList)
{
FirstName.Text = node.SelectSingleNode("FirstName").InnerText;
LastName.Text = node.SelectSingleNode("LastName").InnerText;
DateOfBirth.Text = node.SelectSingleNode("DateOfBirth").InnerText;
Nationality.Text = node.SelectSingleNode("Nationality").InnerText;
Passport.Text=node.SelectSingleNode("Passport").InnerText;
Address.Text = node.SelectSingleNode("Address").InnerText;
Phone.Text = node.SelectSingleNode("phone").InnerText;
您可以将此作为示例。这就是我对天气的看法。这和你试图做的事情是一样的,只是使用了不同的方法。我展示的代码就是一个例子
private void GetWeather()
{
string query = string.Format("http://weather.yahooapis.com/forecastrss?w=12773086");
XmlDocument wData = new XmlDocument();
wData.Load(query);
XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather.forecast", manager);
Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;
Visibility = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["visibility"].Value;
Pressure = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["pressure"].Value;
Rising = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["rising"].Value;
Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;
State = channel.SelectSingleNode("yweather:location", manager).Attributes["region"].Value;
Windspeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
Windchill = channel.SelectSingleNode("yweather:wind", manager).Attributes["chill"].Value;
TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;
TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;
TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value;
Distance = channel.SelectSingleNode("yweather:units", manager).Attributes["distance"].Value;
Sunrise = channel.SelectSingleNode("yweather:astronomy", manager).Attributes["sunrise"].Value;
Sunset = channel.SelectSingleNode("yweather:astronomy", manager).Attributes["sunset"].Value;
Speed = channel.SelectSingleNode("yweather:units", manager).Attributes["speed"].Value;
BPressure = channel.SelectSingleNode("yweather:units", manager).Attributes["pressure"].Value;
Degrees = channel.SelectSingleNode("yweather:units", manager).Attributes["temperature"].Value;
Country = channel.SelectSingleNode("yweather:location", manager).Attributes["country"].Value;
}
然后看看“温度”这个词。为了使用此功能,您需要添加
string Temperature;
然后您可以使用它,如下所示
case "whats it like outside":
GetWeather();
Alexis.SpeakAsync("Currently it is, " + Condition + "with a high of " + Temperature + Degrees + ", there is a humidity of " + Humidity + "percent, and a windspeed of " + Windspeed + Speed);
break;
这是我每天使用的应用程序。因此,这应该给您一个想法。从我所能看到的代码来看,您似乎是在集合中的每个用户之间循环,而不是选择一个特定的用户。可以使用linq形成查询,根据元素中包含的值选择特定元素,如下所示:
class Program
{
static void Main(string[] args)
{
var xdoc =
XDocument.Load(
@"~\Examples\user.xml");
var users = xdoc.Descendants("userInfo");
// get the specific user
var user = users.Where(x => x.Element("Passport").Value == "1");
// get the value from each child element in the selected user
foreach(var element in user.Elements())
{
Console.WriteLine(element.Value);
}
Console.ReadLine();
}
}
我假设passport字段是用于linq查询的唯一标识符,如您所见,这是一个非常简短的示例
我使用的xml:
<UsersInfo>
<userInfo>
<FirstName>First</FirstName>
<LastName>Last</LastName>
<Passport>1</Passport>
</userInfo>
<userInfo>
<FirstName>Second</FirstName>
<LastName>Last</LastName>
<Passport>2</Passport>
</userInfo>
</UsersInfo>
您需要匹配UserInfo节点中的用户名和密码。因此,只需在其中放置一个if构造,假设用户名和密码存储在XML中
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo.xml");
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/UsersInfo/UserInfo");
foreach (XmlNode node in nodeList)
{
if (node.SelectSingleNode("Username").InnerText == username &&
node.SelectSingleNode("Password").InnertText == password)
{
FirstName.Text = node.SelectSingleNode("FirstName").InnerText;
LastName.Text = node.SelectSingleNode("LastName").InnerText;
DateOfBirth.Text = node.SelectSingleNode("DateOfBirth").InnerText;
Nationality.Text = node.SelectSingleNode("Nationality").InnerText;
Passport.Text=node.SelectSingleNode("Passport").InnerText;
Address.Text = node.SelectSingleNode("Address").InnerText;
Phone.Text = node.SelectSingleNode("phone").InnerText;
}
}
然后,您将只匹配用户名和密码与输入的用户名/密码变量匹配的用户
你还应该考虑把密码散列,而不是把它存储在纯文本中,如果你还没有这样做。
玛丽,你提供的代码不足以定义什么不起作用。通过登录并在表单上显示当前用户来显示代码您可以显示您的XML模板吗?该文件中有多少用户信息?