[RESOLVED] Reading XML file trouble, exception after first entry
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] Reading XML file trouble, exception after first entry

  1. #1
    Join Date
    Oct 2010
    Posts
    8

    [RESOLVED] Reading XML file trouble, exception after first entry

    Hello,

    I'm able to write out an XML file that put the values of a few textboxs in the XML file for saving, but when I try to read it back in, it reads in the first line and then gives me an exception and stops reading in the rest of the lines.

    Here is the code:

    Code:
    private: System::Void openToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
    {
    Stream^ myStream;
    XmlTextReader^ xmlTxtReader;
    
    try 
    {
    if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)		
    {
    if ( (myStream = openFileDialog->OpenFile()) != nullptr )
    {
    myStream->Close();
    						
    xmlTxtReader = gcnew XmlTextReader(this->openFileDialog->FileName);
    						
    while (xmlTxtReader->Read())
    {
    							
    if(xmlTxtReader->Name->Equals("Info") && xmlTxtReader->NodeType == XmlNodeType::Element)
    {
    xmlTxtReader->Read();
    
    this->BoardNameTextBox1->Text = xmlTxtReader->ReadElementString("BoardNameTextBox1");
    this->BoardNumberTextBox1->Text = xmlTxtReader->ReadElementString("BoardNumberTextBox1");
    this->AssemblyNumberTextBox1->Text = xmlTxtReader->ReadElementString("AssemblyNumberTextBox1");
    this->NumberPlotFilesNumericUpDown1->Text = xmlTxtReader->ReadElementString("NumberPlotFilesNumericUpDown1");
    this->ElectricalLayersNumericUpDown1->Text = xmlTxtReader->ReadElementString("ElectricalLayersNumericUpDown1");
    this->BoardMaterialComboBox1->Text = xmlTxtReader->ReadElementString("BoardMaterialComboBox1");
    this->BoardSizeMaskedTextBox1->Text = xmlTxtReader->ReadElementString("BoardSizeMaskedTextBox1");
    this->BoardThicknessTextBox1->Text = xmlTxtReader->ReadElementString("BoardThicknessTextBox1");
    this->BoardThicknessTextBox2->Text = xmlTxtReader->ReadElementString("BoardThicknessTextBox2");
    this->BoardFinishComboBox1->Text = xmlTxtReader->ReadElementString("BoardFinishComboBox1");
    this->DesignerNameTextBox1->Text = xmlTxtReader->ReadElementString("DesignerNameTextBox1");
    this->PhoneNumberMaskedTextBox1->Text = xmlTxtReader->ReadElementString("PhoneNumberMaskedTextBox1");
    this->FileDateTimePicker1->Text = xmlTxtReader->ReadElementString("FileDateTimePicker1");
    }		       
    }
    }
    }
    }
    catch(XmlException ^)
    {
    MessageBox::Show(L"The file name you provided is not valid");
    }
    __finally
    {
    xmlTxtReader->Close();
    }
    }
    Here is my XML data it's trying to read in:

    Code:
     <?xml version="1.0" encoding="utf-8" ?> 
    - <Board>
    - <Info>
     <BoardNameTextBox1>asdfghjklqwe</BoardNameTextBox1> 
     <AssemblyNumberTextBox1>098756534</AssemblyNumberTextBox1> 
     <BoardNumberTextBox1>132456789</BoardNumberTextBox1> 
     <NumberPlotFilesNumericUpDown1>1</NumberPlotFilesNumericUpDown1> 
     <ElectricalLayersNumericUpDown1>16</ElectricalLayersNumericUpDown1> 
     <BoardMaterialComboBox1>FR-4</BoardMaterialComboBox1> 
     <BoardSizeMaskedTextBox1>1.231" x 23.123"</BoardSizeMaskedTextBox1> 
     <BoardThicknessTextBox1>92.6 MILS</BoardThicknessTextBox1> 
     <BoardThicknessTextBox2> 8.0</BoardThicknessTextBox2> 
     <BoardFinishComboBox1>ENIG</BoardFinishComboBox1> 
     <DesignerNameTextBox1>Your Name</DesignerNameTextBox1> 
     <PhoneNumberMaskedTextBox1>(xxx) xxx-xxxx</PhoneNumberMaskedTextBox1> 
     <FileDateTimePicker1>10/19/2010</FileDateTimePicker1> 
     </Info>
     </Board>
    Can anyone please tell me how to get it to read in all the fields instead of crashing after just the first one?

    Thanks for any help.

  2. #2
    Join Date
    Oct 2010
    Posts
    8

    Red face Re: Reading XML file trouble, exception after first entry

    Doh. Nevermind, I figured it out. I had two lines reversed.

    Code:
    this->BoardNumberTextBox1->Text = xmlTxtReader->ReadElementString("BoardNumberTextBox1");
    this->AssemblyNumberTextBox1->Text = xmlTxtReader->ReadElementString("AssemblyNumberTextBox1");
    should be:

    Code:
    this->AssemblyNumberTextBox1->Text = xmlTxtReader->ReadElementString("AssemblyNumberTextBox1");
    this->BoardNumberTextBox1->Text = xmlTxtReader->ReadElementString("BoardNumberTextBox1");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center