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>
4 Attachment(s)
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>
Re: Data at the root level is invalid. Line 1, position 1
You want to use LOAD, not LOADXML.
Code:
xmldoc.Load(strFile)
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
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
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>
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
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
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
You want to use LOAD, not LOADXML.
Code:
xmldoc.Load(strFile)