2016-03-18 15 views
-1

私はC#を学習しており、問題に取り組んでいます。ユーザーの情報が異なるXMLファイルを作成しました。彼のログイン時に特定のユーザー情報を表示するのに問題があります。ユーザー名とパスワードを使用した人の情報を表示したいと思います。C#ログインユーザーのXMLファイル表示情報

最初のユーザーがログインしても、すぐに2番目の情報が表示されるためです。

これは、情報を表示するための私のコードですが、それは正しく

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; 
+2

あなたが提供したコードは、動作しないものを定義するには不十分です。ログインしてコードを表示し、現在のユーザーをフォームに表示する –

+0

XMLテンプレートを表示できますか?そのファイルにはどのくらいのユーザー情報がありますか? –

答えて

0

あなたは一例として、これを使用することができますが動作しません。これは私が天気を引き出す方法です。あなたのやっていることと同じことですが、別の方法を使用しています。私が示していたコードは、次に単語温度を見て例

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; 
    } 

です。これを使用するためには、

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; 

下に示すなど、あなたがそれを使用することができ、これは私のアプリでは、私が毎日使っている

string Temperature; 

を追加します。だからこれはあなたに考えを与えるはずです。

0

少しずつ見ると、特定のユーザーを選択するのではなく、コレクションの各ユーザーをループしているように見えることがあります。あなたは、要素に含まれる値に基づいて特定の要素を選択するには、このような何かを、クエリを形成するために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(); 
    } 
} 

私はパスポートのフィールドは、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> 
0

をあなたがのUserInfoノードからユーザー名とパスワードと一致する必要があるとしています。 (ユーザ名とパスワードは、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; 
    } 
} 

を置きますユーザー名/パスワード変数)。

また、パスワードを平文で保存するのではなく、パスワードをハッシュすることも検討してください。

関連する問題