locked
Root element is missing RRS feed

  • Question

  • Hello, I have an XML file and I want to make a log in base on this file. Here's my code for registration dates in the XML file :

    private void button1_Click(object sender, EventArgs e)
            {
                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("Continuati?");
                }
                XmlElement root = doc.DocumentElement;
                XmlElement Subroot = doc.CreateElement("Angajat");
                XmlElement nume = doc.CreateElement("Nume_Prenume");
                XmlElement id = doc.CreateElement("ID");
                XmlElement parola = doc.CreateElement("Parola");
                XmlElement telefon = doc.CreateElement("Nr_de_telefon");
                XmlElement adresa = doc.CreateElement("Adresa");
                nume.InnerText = textBox1.Text;
                id.InnerText = textBox2.Text;
                parola.InnerText = textBox3.Text;
                telefon.InnerText = textBox4.Text;
                adresa.InnerText = textBox5.Text;
                Subroot.AppendChild(nume);
                Subroot.AppendChild(id);
                Subroot.AppendChild(parola);
                Subroot.AppendChild(telefon);
                Subroot.AppendChild(adresa);

                root.AppendChild(Subroot);
                doc.AppendChild(root);
                doc.Save(path);
                MessageBox.Show("Datele au fost introduse cu succes");
         }

    Here is the xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <Persoane>
      <Angajat>
        <Nume_Prenume>Horatiu Necula</Nume_Prenume>
        <ID>horatiu</ID>
        <Parola>123</Parola>
        <Nr_de_telefon>0723626741</Nr_de_telefon>
        <Adresa>Valenii de munte ,PH</Adresa>
      </Angajat>
    </Persoane>

    and here is the code for the log in :

    private void button2_Click(object sender, EventArgs e)
            {
                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");
                    }
                }

    and when I run it I get the error "Root element is missing" at line > "doc.Load(filename);" Some help ???

    Tuesday, September 13, 2016 6:01 PM

Answers

  • Hi Hooratiu123,

    Thank you for posting here.

    For your question, please try the following code.

    In designer.

    Here is the code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace reboot_element
    {
    	public partial class Form1 : Form
    	{
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void button1_Click(object sender, EventArgs e)
    		{
    			string path = @"C:\Users\wendy\Desktop\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("Continuati?");
    			}
    			XmlElement root = doc.DocumentElement;
    			XmlElement Subroot = doc.CreateElement("Angajat");
    			XmlElement nume = doc.CreateElement("Nume_Prenume");
    			XmlElement id = doc.CreateElement("ID");
    			XmlElement parola = doc.CreateElement("Parola");
    			XmlElement telefon = doc.CreateElement("Nr_de_telefon");
    			XmlElement adresa = doc.CreateElement("Adresa");
    
    			nume.InnerText = textBox1.Text;
    			id.InnerText = textBox2.Text;
    			parola.InnerText = textBox3.Text;
    			telefon.InnerText = textBox4.Text;
    			adresa.InnerText = textBox5.Text;
    			Subroot.AppendChild(nume);
    			Subroot.AppendChild(id);
    			Subroot.AppendChild(parola);
    			Subroot.AppendChild(telefon);
    			Subroot.AppendChild(adresa);
    
    			root.AppendChild(Subroot);
    			doc.AppendChild(root);
    			doc.Save(path);
    			MessageBox.Show("Datele au fost introduse cu succes");
    
    		}
    		private void button2_Click(object sender, EventArgs e)
    		{
    			XmlDocument doc = new XmlDocument();
    
    			string filename = @"C:\Users\wendy\Desktop\XMLFile1.xml";
    
    			doc.Load(filename);
    
    			foreach (XmlNode node in doc.SelectNodes("Persoane"))
    			{
    				String Username = Convert.ToString(node.SelectSingleNode("descendant::Angajat").ChildNodes[2].InnerText);
    				String Password = Convert.ToString(node.SelectSingleNode("descendant::Angajat").ChildNodes[3].InnerText);
    				if (Username == textBox3.Text && Password == textBox4.Text)
    				{
    					//Form a = new Form1();
    					//a.Show();
    					//this.Hide();
    					MessageBox.Show("log in");
    				}
    				else
    				{
    					MessageBox.Show("something is wrong");
    				}
    			}
    		}
    	}
    }
    
    
    

    I hope this would be helpful to you.

    If it solves you problem, please mark as answer.

    If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Sabah ShariqMVP Thursday, September 15, 2016 11:47 AM
    • Marked as answer by Kevin Linq Wednesday, September 21, 2016 4:33 AM
    Thursday, September 15, 2016 1:23 AM

All replies

  • Hey,

    Are you sure that the file path is correct? & file is present in that path?

    Please refer : http://stackoverflow.com/a/25036717/4499378

    Thanks :-)

    Tuesday, September 13, 2016 6:20 PM
  • Hi Hooratiu123,

    Thank you for posting here.

    For your question, please try the following code.

    In designer.

    Here is the code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace reboot_element
    {
    	public partial class Form1 : Form
    	{
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void button1_Click(object sender, EventArgs e)
    		{
    			string path = @"C:\Users\wendy\Desktop\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("Continuati?");
    			}
    			XmlElement root = doc.DocumentElement;
    			XmlElement Subroot = doc.CreateElement("Angajat");
    			XmlElement nume = doc.CreateElement("Nume_Prenume");
    			XmlElement id = doc.CreateElement("ID");
    			XmlElement parola = doc.CreateElement("Parola");
    			XmlElement telefon = doc.CreateElement("Nr_de_telefon");
    			XmlElement adresa = doc.CreateElement("Adresa");
    
    			nume.InnerText = textBox1.Text;
    			id.InnerText = textBox2.Text;
    			parola.InnerText = textBox3.Text;
    			telefon.InnerText = textBox4.Text;
    			adresa.InnerText = textBox5.Text;
    			Subroot.AppendChild(nume);
    			Subroot.AppendChild(id);
    			Subroot.AppendChild(parola);
    			Subroot.AppendChild(telefon);
    			Subroot.AppendChild(adresa);
    
    			root.AppendChild(Subroot);
    			doc.AppendChild(root);
    			doc.Save(path);
    			MessageBox.Show("Datele au fost introduse cu succes");
    
    		}
    		private void button2_Click(object sender, EventArgs e)
    		{
    			XmlDocument doc = new XmlDocument();
    
    			string filename = @"C:\Users\wendy\Desktop\XMLFile1.xml";
    
    			doc.Load(filename);
    
    			foreach (XmlNode node in doc.SelectNodes("Persoane"))
    			{
    				String Username = Convert.ToString(node.SelectSingleNode("descendant::Angajat").ChildNodes[2].InnerText);
    				String Password = Convert.ToString(node.SelectSingleNode("descendant::Angajat").ChildNodes[3].InnerText);
    				if (Username == textBox3.Text && Password == textBox4.Text)
    				{
    					//Form a = new Form1();
    					//a.Show();
    					//this.Hide();
    					MessageBox.Show("log in");
    				}
    				else
    				{
    					MessageBox.Show("something is wrong");
    				}
    			}
    		}
    	}
    }
    
    
    

    I hope this would be helpful to you.

    If it solves you problem, please mark as answer.

    If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Sabah ShariqMVP Thursday, September 15, 2016 11:47 AM
    • Marked as answer by Kevin Linq Wednesday, September 21, 2016 4:33 AM
    Thursday, September 15, 2016 1:23 AM
  • Hi Hooratiu123,

    If it solves your problem, please mark as answer.

    This will make answer searching easier in the forum and be beneficial to community members.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 20, 2016 12:37 PM