469,568 Members | 1,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

How to read XML file without "Root" Node

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 ***
Sep 10 '08 #1
2 10068
Tom Chang wrote:
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/
Sep 10 '08 #2
Martin,
Thanks for your help. It works!
- Rgds, Tom

*** Sent via Developersdex http://www.developersdex.com ***
Sep 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jonathan Wilson | last post: by
2 posts views Thread by John A Grandy | last post: by
5 posts views Thread by TomislaW | last post: by
reply views Thread by damontimm | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.