Connecting Tech Pros Worldwide Forums | Help | Site Map

How to read XML file without "Root" Node

Tom Chang
Guest
 
Posts: n/a
#1: Sep 10 '08
Try to read following type of XML file, but only get data back on first
node:

<log id="1234">
<message type="Action"/>
</log>
<log id="5678">
<message type="Drama"/>
</log>
<log id="1357">
<message type="Art"/>
</log>

Here is my very simple VB program:

Imports System.IO
Imports System.Xml
Dim readWNInfo As XmlTextReader
readWNInfo = New XmlTextReader("test.log")
While readWNInfo.Read()
If readWNInfo.NodeType = XmlNodeType.Element Then
Console.WriteLine("Node:" & readWNInfo.Name & _
", AttributeCount: " & _
readWNInfo.AttributeCount.ToString())
End if
End while
readWNInfo.Close()

If I add <rootat first line, and </rootat the end, the above code
works. Can I make work without modify the original log file?

Thanks for your help in advance,

Tom

*** Sent via Developersdex http://www.developersdex.com ***

Martin Honnen
Guest
 
Posts: n/a
#2: Sep 10 '08

re: How to read XML file without "Root" Node


Tom Chang wrote:
Quote:
Try to read following type of XML file, but only get data back on first
node:
>
<log id="1234">
<message type="Action"/>
</log>
<log id="5678">
<message type="Drama"/>
</log>
<log id="1357">
<message type="Art"/>
</log>
If you have .NET 2.0 or later then use
Dim set as New XmlReaderSettings()
set.ConformanceLevel = ConformanceLevel.Fragment
Using reader As XmlReader = XmlReader.Create("test.log", set)
While reader.Read()
'read out properties here
End While
End Using

See
http://msdn.microsoft.com/en-us/libr...ancelevel.aspx

That way you can process fragments of XML that don't have a root element.



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Tom Chang
Guest
 
Posts: n/a
#3: Sep 10 '08

re: How to read XML file without "Root" Node


Martin,
Thanks for your help. It works!
- Rgds, Tom

*** Sent via Developersdex http://www.developersdex.com ***
Closed Thread