XML Parsing Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nexus024
    New Member
    • Feb 2007
    • 4

    XML Parsing Error

    I am writing a program for one of my C++ classes at school and have gotten an error that I can't debug. What I am trying to do is write a C++ program that parses an XML file and displays to console all the different elements of the XML file. The error message I continuously keep getting is "Data at the root level is invalid. Line 1, Position 1." Please advise!

    Code:
    #include "stdafx.h"
    #using <mscorlib.dll>
    #using <System.xml.dll>
    
    
    
    using namespace System;
    using namespace System::Xml;
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	
    	try
      {
    	
    	//load xml file into reader
    	XmlTextReader*  txtreader = new XmlTextReader (argv[0]);
    	XmlValidatingReader*  XmlReader = new XmlValidatingReader(txtreader);
    
    	//while still reading a line from XML file
        while (XmlReader->Read())
        {
            switch (XmlReader->NodeType)
            {
    			
    			case XmlNodeType::Element: // The node is an element.
    				
    				Console::Write(XmlReader->Name);
    				Console::Write("\t");
    				
    				//read the next attribute of the element
                    while (XmlReader->MoveToNextAttribute()) 
    				Console::Write(" {0}='{1}'", XmlReader->Name, XmlReader->Value);
    				
                    break;
    			case XmlNodeType::Text: //Display the text in each element.
    				Console::WriteLine (XmlReader->Value);
    
                    break;
    			case XmlNodeType::EndElement: //Display the end of the element.
    				
                    break;
            }
        }
        Console::ReadLine();
    
    
      }
      catch (Exception *e)
      {
        System::Console::WriteLine("load problem");
        System::Console::WriteLine(e->Message);
    
      }
    
      
    
      return 0;
    }
    stdafx.cpp
    Code:
    #include "stdafx.h"
    stdafx.h
    Code:
    #pragma once
    
    
    #include <iostream>
    #include <tchar.h>
  • nexus024
    New Member
    • Feb 2007
    • 4

    #2
    anyone know?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by nexus024
      I am writing a program for one of my C++ classes at school and have gotten an error that I can't debug. What I am trying to do is write a C++ program that parses an XML file and displays to console all the different elements of the XML file. The error message I continuously keep getting is "Data at the root level is invalid. Line 1, Position 1." Please advise!

      Code:
      #include "stdafx.h"
      #using <mscorlib.dll>
      #using <System.xml.dll>
       
       
       
      using namespace System;
      using namespace System::Xml;
       
       
       
       
      int _tmain(int argc, _TCHAR* argv[])
      {
       
       
      	try
      {
       
      	//load xml file into reader
      	XmlTextReader* txtreader = new XmlTextReader (argv[0]);
      	XmlValidatingReader* XmlReader = new XmlValidatingReader(txtreader);
       
      	//while still reading a line from XML file
      while (XmlReader->Read())
      {
      switch (XmlReader->NodeType)
      {
       
      			case XmlNodeType::Element: // The node is an element.
       
      				Console::Write(XmlReader->Name);
      				Console::Write("\t");
       
      				//read the next attribute of the element
      while (XmlReader->MoveToNextAttribute()) 
      				Console::Write(" {0}='{1}'", XmlReader->Name, XmlReader->Value);
       
      break;
      			case XmlNodeType::Text: //Display the text in each element.
      				Console::WriteLine (XmlReader->Value);
       
      break;
      			case XmlNodeType::EndElement: //Display the end of the element.
       
      break;
      }
      }
      Console::ReadLine();
       
       
      }
      catch (Exception *e)
      {
      System::Console::WriteLine("load problem");
      System::Console::WriteLine(e->Message);
       
      }
       
       
       
      return 0;
      }
      stdafx.cpp
      Code:
      #include "stdafx.h"
      stdafx.h
      Code:
      #pragma once
       
       
      #include <iostream>
      #include <tchar.h>
      Check that the XML is well formed(Run it on a browser or something). then check that your path to the XML file is correct.

      Comment

      Working...