2016-09-19 6 views
1

大きなプロジェクトではほぼ完了していますが、ここに最後のエラーがあります。だから私は、私はいくつかのデータを追加することができるXMLファイルを作った、ここではコードです:XMLによるエラーログイン

 string path = "XMLFile1.xml"; 
     XmlDocument doc = new XmlDocument(); 
     if (!System.IO.File.Exists(path)) 
     { 
      MessageBox.Show("lmge;lm"); 
      XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"); 
      XmlComment comment = doc.CreateComment("This is an XML Generated File"); 
      doc.AppendChild(declaration); 
      doc.AppendChild(comment); 
     } 
     else 
     { 
      doc.Load(path); MessageBox.Show("Everyting is right?"); 
     } 
     XmlElement root = doc.DocumentElement; 
     XmlElement Subroot = doc.CreateElement("Angajat"); 
     XmlElement name = doc.CreateElement("Name"); 
     XmlElement id = doc.CreateElement("ID"); 
     XmlElement password = doc.CreateElement("Password"); 
     XmlElement phone = doc.CreateElement("phone_nr"); 
     XmlElement address = doc.CreateElement("Address"); 

     nume.InnerText = textBox1.Text; 
     id.InnerText = textBox2.Text; 
     password.InnerText = textBox3.Text; 
     phone.InnerText = textBox4.Text; 
     address.InnerText = textBox5.Text; 
     Subroot.AppendChild(name); 
     Subroot.AppendChild(id); 
     Subroot.AppendChild(password); 
     Subroot.AppendChild(phone); 
     Subroot.AppendChild(address); 

     root.AppendChild(Subroot); 
     doc.AppendChild(root); 
     doc.Save(path); 
     MessageBox.Show("Succes!"); 

そして今、私は私がエラー

 XmlDocument doc = new XmlDocument(); 
     string filename = @"D:\Poriecte Visual\INFO2017\INFO2017\XMLFile1.xml"; 

     doc.Load(filename); 

     foreach (XmlNode node in doc.SelectNodes("persoane")) 
     { 
      String Username = node.SelectSingleNode("ID").InnerText; 
      String Password = node.SelectSingleNode("Password").InnerText; 

      if (Username == textBox3.Text && Password == textBox4.Text) 
      { 
       Form a = new Form4(); 
       a.Show(); 
       this.Hide(); 
      } 
      else 
      { 
       MessageBox.Show("Something is wrong"); 
      } 

に取得ログインを作り、私はこのエラーを取得する:enter image description here

私のXMLファイルを次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<Persoane> 
<Angajat> 
    <Name>Horatiu Necula</Name> 
    <ID>horatiu</ID> 
    <Password>123</Password> 
    <Phone_nr>0723626741</Phone_nr> 
    <Address>Valenii de munte ,PH</Address> 
</Angajat> 
</Persoane> 

は私を助けて、私は最後の数日多くのことを検索しました\

EDIT:誰かが助けてくれました。パスは正しくありませんでしたが、今は "オブジェクト参照がオブジェクトのインスタンスに設定されていません"というエラーが表示されます(string Username = node .SelectSingleNode( "ID")。InnerText; )

+0

これは実際にXMLファイルの外観ですか?なぜすべてのタグに空白がありますか? – Phylogenesis

+0

@Phylogenesisタグが消える空白を削除した場合、ビジュアルには表示されません –

+0

有効なXMLで質問を編集しました。 – Phylogenesis

答えて

0

xmlファイルへのパスが正しいですか? このエラーは、パスのドキュメントが実際に空の場合に発生することがあります。 XMLファイルをFileStreamでロードすることをお勧めします。具体的にはhereと記述されています。これは、ファイルが利用できないか、アクセスできない場合に、より詳細なエラーが設定されるためです。

関連する問題