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