469,919 Members | 2,166 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,919 developers. It's quick & easy.

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!

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #using <mscorlib.dll>
  3. #using <System.xml.dll>
  4.  
  5.  
  6.  
  7. using namespace System;
  8. using namespace System::Xml;
  9.  
  10.  
  11.  
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15.  
  16.  
  17.     try
  18.   {
  19.  
  20.     //load xml file into reader
  21.     XmlTextReader*  txtreader = new XmlTextReader (argv[0]);
  22.     XmlValidatingReader*  XmlReader = new XmlValidatingReader(txtreader);
  23.  
  24.     //while still reading a line from XML file
  25.     while (XmlReader->Read())
  26.     {
  27.         switch (XmlReader->NodeType)
  28.         {
  29.  
  30.             case XmlNodeType::Element: // The node is an element.
  31.  
  32.                 Console::Write(XmlReader->Name);
  33.                 Console::Write("\t");
  34.  
  35.                 //read the next attribute of the element
  36.                 while (XmlReader->MoveToNextAttribute()) 
  37.                 Console::Write(" {0}='{1}'", XmlReader->Name, XmlReader->Value);
  38.  
  39.                 break;
  40.             case XmlNodeType::Text: //Display the text in each element.
  41.                 Console::WriteLine (XmlReader->Value);
  42.  
  43.                 break;
  44.             case XmlNodeType::EndElement: //Display the end of the element.
  45.  
  46.                 break;
  47.         }
  48.     }
  49.     Console::ReadLine();
  50.  
  51.  
  52.   }
  53.   catch (Exception *e)
  54.   {
  55.     System::Console::WriteLine("load problem");
  56.     System::Console::WriteLine(e->Message);
  57.  
  58.   }
  59.  
  60.  
  61.  
  62.   return 0;
  63. }
stdafx.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
stdafx.h
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3.  
  4. #include <iostream>
  5. #include <tchar.h>
Feb 1 '07 #1
2 1828
anyone know?
Feb 1 '07 #2
r035198x
13,262 8TB
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!

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #using <mscorlib.dll>
  3. #using <System.xml.dll>
  4.  
  5.  
  6.  
  7. using namespace System;
  8. using namespace System::Xml;
  9.  
  10.  
  11.  
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15.  
  16.  
  17.     try
  18. {
  19.  
  20.     //load xml file into reader
  21.     XmlTextReader* txtreader = new XmlTextReader (argv[0]);
  22.     XmlValidatingReader* XmlReader = new XmlValidatingReader(txtreader);
  23.  
  24.     //while still reading a line from XML file
  25. while (XmlReader->Read())
  26. {
  27. switch (XmlReader->NodeType)
  28. {
  29.  
  30.             case XmlNodeType::Element: // The node is an element.
  31.  
  32.                 Console::Write(XmlReader->Name);
  33.                 Console::Write("\t");
  34.  
  35.                 //read the next attribute of the element
  36. while (XmlReader->MoveToNextAttribute()) 
  37.                 Console::Write(" {0}='{1}'", XmlReader->Name, XmlReader->Value);
  38.  
  39. break;
  40.             case XmlNodeType::Text: //Display the text in each element.
  41.                 Console::WriteLine (XmlReader->Value);
  42.  
  43. break;
  44.             case XmlNodeType::EndElement: //Display the end of the element.
  45.  
  46. break;
  47. }
  48. }
  49. Console::ReadLine();
  50.  
  51.  
  52. }
  53. catch (Exception *e)
  54. {
  55. System::Console::WriteLine("load problem");
  56. System::Console::WriteLine(e->Message);
  57.  
  58. }
  59.  
  60.  
  61.  
  62. return 0;
  63. }
stdafx.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
stdafx.h
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3.  
  4. #include <iostream>
  5. #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.
Feb 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Gerrit Holl | last post: by
16 posts views Thread by Terry | last post: by
3 posts views Thread by David Svoboda | last post: by
13 posts views Thread by Chris Carlen | last post: by
reply views Thread by =?Utf-8?B?VWxmIFRob3JzZW4=?= | last post: by
2 posts views Thread by Felipe De Bene | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.