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.
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.
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
Originally Posted by HanneSThEGreaT
Please post [CODE] tags when posting code please. It is explained here :
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.