Forums: Back End:

 

"XML - Root Element is Missing" (.NET Error Message)

first
 

JimmyTheGent "XML - Root Element is Missing" (.NET Error Message)

I am using a .NET to look through some XML files and show on the page some output if it finds what it is looking for.

I keep getting the error System.Xml.XMLException: Root element is missing.

I have played with this for ages and I am certain the XML is fine.... Could someone lend a hand and see if they can see a problem? Would really appreciate the help.

JTG

Example XML File & Program Code.



<?xml version="1.0" ?>
- <LogonList>
- <User_logon>
<Logon_Date>08/05/2008 08:20:28</Logon_Date>
<User>whyte_s</User>
<Machinename>ST-15086</Machinename>
</User_logon>
- <User_logon>
<Logon_Date>09/05/2008 08:19:05</Logon_Date>
<User>whyte_s</User>
<Machinename>ST-15086</Machinename>
</User_logon>
- <User_logon>
<Logon_Date>12/05/2008 08:13:06</Logon_Date>
<User>whyte_s</User>
<Machinename>ST-15086</Machinename>
</User_logon>
</LogonList>





Try
'create an array of XML files in directory
Dim Files() As String = Directory.GetFiles(FilePath(), "*.xml")


Dim strTemp As String
For Each strTemp In Files

'create variables for reading XML
Dim XMLDoc As XmlDocument = New XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode



XMLDoc.Load(strTemp) 'Load xml file into XMLDoc
m_nodelist = XMLDoc.SelectNodes("LogonList/User_logon") 'Get list of User_Logon nodes


'Loop through the file and extract the info if it exists
For Each m_node In m_nodelist
Dim strDate As String = CStr(m_node.ChildNodes.Item(0).InnerText)
Dim strUser As String = CStr(m_node.ChildNodes.Item(1).InnerText)
Dim strMachine As String = CStr(m_node.ChildNodes.Item(2).InnerText)

If strMachine = Pcname() Then
Output() = Output & strUser & " on " & strDate & "<br>"
Exit For
End If

Next

Next
Catch ex As Exception
'Errors generated by exiting loop so no catch wanted.
MsgBox(ex.ToString)

End Try

 

Stickman

//edit ...actually I'm talking crap...nm.

StickBlog - random developer stuff
quote
 

jamiec

What line of that code do you get that error on?

 

Stickman

First thing to do, check that it really is trying to parse the content you think it is -- it might be that the XmlDocument.Load method isn't doing what you think.

StickBlog - random developer stuff
quote
 

JimmyTheGent

I should have added. I do get the expected output however its accompanied by the error. I *could* ignore the error but in the past that has not proven wise.

I have tried using a breakpoint on the second last next and stepped in. It does loop through all the xml files checking them but after about 100-130 files have been checked it throws the error.

All the XML files are generated by an application so have identical nodes etc. I tried deleting what I thought might be culprit XML file (the one after the last one it read, as i figured that caused the error) but it still throws the error on the next one anyway.

 

Stickman

Hmm...perhaps you should check the output of this line:


Dim Files() As String = Directory.GetFiles(FilePath(), "*.xml")


to make sure it's not trying to read any files/directories that it shouldn't.

StickBlog - random developer stuff
quote
 

JimmyTheGent

I checked the output as you suggested and it is only checking the directory it is supposed to.

As I said above, usually after it checks a number of files it seems to crash with that error....well I know why now. someone has manually created an XML file it seems with no data. It cannot have been generated by the system as it would have at least one entry...

So a name.xml file with no data means no elements!

I just so happened to notice a file had 0k size.

Thanks very much for your help both of you. I am getting better at the .NET stuff so before long I will repay your efforts by helping others in the same way big grin

JTG

 
first
 

Forums: Back End: "XML - Root Element is Missing" (.NET Error Message)

 
New Post
 
You must be logged in to post