CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    27

    Data at the root level is invalid. Line 1, position 1

    Wondering what is it that I am doing wrong (and how I can fix it) ... I am trying to learn how to read XML file and created a little program below to start testing what I have read but keep getting error "Data at the root level is invalid. Line 1, position 1"

    The line where the error is coming from is the line "xmldoc.LoadXml(strFile)".. any ideas?

    Thank you so much for all the help you could give.

    Gratefully,
    Matt

    Code:
      Private Sub cmdReadXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)   
    Handles cmdReadXML.Click
          Dim clsk As New clsHx
          clsk.ReadXML("e:\xml\mama.xml")
      End Sub
    
      Below is the a subroutine in my clsHx class:
      Public Sub ReadXML(ByVal strFile As String)
          Dim xmldoc As New XmlDocument
    
          xmldoc.LoadXml(strFile)
          MsgBox(xmldoc.GetElementsByTagName("myfirstdata"))
          MsgBox(xmldoc.GetElementsByTagName("mySecondData"))
    
      End Sub
    Below is the content of the XML file which is located in e:\xml\mama.xml:
    Code:
      <?xml version="1.0" ?> 
      <myData>
      <myrecord>
      <myfirstdata>String for my second element</myfirstdata> 
      <mySecondData>Second data element here</mySecondData> 
      </myrecord>
      <myrecord>
      <myfirstdata>record2 for my second element</myfirstdata> 
      <mySecondData>record2mySec Second data element here</mySecondData> 
      <myThirdData>recorderData data element here</myThirdData> 
      </myrecord>
      </myData>
    Last edited by HanneSThEGreaT; May 6th, 2010 at 08:34 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Data at the root level is invalid. Line 1, position 1

    Please post [CODE] tags when posting code please. It is explained here :

    http://www.codeguru.com/forum/showthread.php?t=403073

    As to your issue, I'm curious to know why your'e not using the XMLReader / Writer objects??

    Have a look at the attachments, it should help. Just copy the necessary XML files to the C:\

    You may also have a problem with the format of your XML document. For example, one of my XML files look like :

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!--information about this book-->
    <book isbn="0-672-32549-7">
      <title>Start</title>
      <authors>
        <author>D</author>
        <author>A</author>
        <author>E</author>
        <author>J</author>
      </authors>
      <chapters>
        <chapter id="1" topic="A" />
        <chapter id="2" topic="B" />
        <chapter id="3" topic="C" />
        <chapter id="4" topic="D" />
        <chapter id="5" topic="E" />
        <chapter id="6" topic="F" />
        <chapter id="7" topic="G" />
        <chapter id="8" topic="H" />
        <chapter id="9" topic="I" />
        <chapter id="10" topic="J" />
        <chapter id="K" topic="L" />
      </chapters>
    </book>
    Attached Files Attached Files
    Last edited by HanneSThEGreaT; May 6th, 2010 at 08:55 AM.

  3. #3
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: Data at the root level is invalid. Line 1, position 1

    You want to use LOAD, not LOADXML.

    Code:
    xmldoc.Load(strFile)
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  4. #4
    Join Date
    Dec 2007
    Posts
    234

    Re: Data at the root level is invalid. Line 1, position 1

    To go a little further, Load takes a file name to load the XML from.... LoadXML takes a string formated as XML as its parameter...

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  5. #5
    Join Date
    Apr 2009
    Posts
    27

    Thumbs up Re: Data at the root level is invalid. Line 1, position 1

    Thank you so much for the attachments ... I have proceeded with your advise to use the XmlTextReader and revised my little code ... and now everything works like a charm ... thx again

    Public Sub ReadXML(ByVal strFile As String)
    Dim xmlrdr As New XmlTextReader(strFile)
    Do While xmlrdr.Read
    Select Case xmlrdr.NodeType
    Case Xml.XmlNodeType.Element
    Case Xml.XmlNodeType.Text
    MsgBox(xmlrdr.Value)
    Case Xml.XmlNodeType.EndElement
    MsgBox(xmlrdr.Name)
    End Select
    Loop
    End Sub

    Gratefully,
    Matt


    Quote Originally Posted by HanneSThEGreaT View Post
    Please post [CODE] tags when posting code please. It is explained here :

    http://www.codeguru.com/forum/showthread.php?t=403073

    As to your issue, I'm curious to know why your'e not using the XMLReader / Writer objects??

    Have a look at the attachments, it should help. Just copy the necessary XML files to the C:\

    You may also have a problem with the format of your XML document. For example, one of my XML files look like :

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!--information about this book-->
    <book isbn="0-672-32549-7">
      <title>Start</title>
      <authors>
        <author>D</author>
        <author>A</author>
        <author>E</author>
        <author>J</author>
      </authors>
      <chapters>
        <chapter id="1" topic="A" />
        <chapter id="2" topic="B" />
        <chapter id="3" topic="C" />
        <chapter id="4" topic="D" />
        <chapter id="5" topic="E" />
        <chapter id="6" topic="F" />
        <chapter id="7" topic="G" />
        <chapter id="8" topic="H" />
        <chapter id="9" topic="I" />
        <chapter id="10" topic="J" />
        <chapter id="K" topic="L" />
      </chapters>
    </book>

  6. #6
    Join Date
    Apr 2009
    Posts
    27

    Smile Re: Data at the root level is invalid. Line 1, position 1

    Thanks for the info ... I'm learning alot in this experience I really appreciate it

    Quote Originally Posted by TechGnome View Post
    To go a little further, Load takes a file name to load the XML from.... LoadXML takes a string formated as XML as its parameter...

    -tg

  7. #7
    Join Date
    Apr 2009
    Posts
    27

    Smile Re: Data at the root level is invalid. Line 1, position 1

    I tried as you have advised ... ie.. replaced LOADXML with LOAD and exactly as you have predicted the original error message that I reported "invalid. Line 1, position 1" ... vanished instantly.

    Thanks,
    Matt


    Quote Originally Posted by Craig Gemmill View Post
    You want to use LOAD, not LOADXML.

    Code:
    xmldoc.Load(strFile)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured