Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I have an error in my code, i want to deserialize different XML Files and open it in textboxes, the problem i had is when it takes the file path, i Think that there is where the error is, because it takes a one exact file path(ex.):
FileStream FS = new FileStream("example.analy", FileMode.Open, FileAccess.Read);

But i replaced it with this
FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

To read the files that were selected in the openfiledialog.
This is my complete code:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
          {
              if (File.Exists(openFileDialog1.FileName))
              {
              if ((openFileDialog1.OpenFile()) != null)
              {
                      if (openFileDialog1.FileName != null)
                      {
                          openFileDialog1.Filter = "Analytica Files | *.analy";
                          XmlSerializer XS = new XmlSerializer(typeof(Reminders.Information));

                          FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                          Information Info = (Information)XS.Deserialize(FS);
                          XS.Deserialize(FS);
                          Settings.Default.Counter++;
                          Settings.Default.Save();

The problem is that when I try to open a file, appears the next error:
System.InvalidOperationException: 'Error en el documento XML (0, 0).'
Inner Exception
XmlException: Missing Root Element.

What I have tried:

This is my complete code:
<pre>  if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(openFileDialog1.FileName))
                {              
                if ((openFileDialog1.OpenFile()) != null)
                {
                        if (openFileDialog1.FileName != null)
                        {
                            openFileDialog1.Filter = "Analytica Files | *.analy";
                            XmlSerializer XS = new XmlSerializer(typeof(Reminders.Information));
                         
                            FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                            Information Info = (Information)XS.Deserialize(FS);
                            XS.Deserialize(FS);
                            Settings.Default.Counter++;
                            Settings.Default.Save();

Posted
Comments
Ziee-M 4-Jun-18 8:30am    
Hi, The problem is in the xml file not in the code (based on the error).
Somthing wrong with your main root at line 0 postion 0
[no name] 4-Jun-18 9:01am    
If you post the XML we might be able to help more.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900