Search code examples
xmlvb.netif-statementreader

Logic to reading an XML file in vb.net


I have an XML file that i am trying to read and rewrite (for later manipulation) to a new text file using vb.net.

I am able to read the XML successfully and print it out, but i am having problems getting the correct with then start and end tags of each element/attribute.

I am using an 'xmlNodeReader' to read the document by getting the name and value of each read. Then a select case with xmlNodeType.Element or xmlNodeType.EndElement

How do I get the correct logic to solve this, as some XML lines are in the form <Server Type="PropertyDefinitions"> and others are in the form <Server Type="aServerName"/>

Ive tried a if loop for endEntity among other things but none seemed to work. Here is (part of) my code which shows the read and write function. If I haven't provided enough information, please let me know.

     Dim reader As XmlNodeReader = New XmlNodeReader(document)
     Dim result As New StringBuilder
      While reader.Read
        Select Case reader.NodeType

            Case XmlNodeType.Element
            result.Append("<" & reader.Name)

                    If reader.HasAttributes Then
                        While reader.MoveToNextAttribute()
                            result.Append(" " + reader.Name + "=" + Chr(34) + reader.Value + Chr(34))
                        End While

                        If XmlNodeType.EndEntity Then
                            result.Append("/>")
                        End If
                    Else
                        If XmlNodeType.Entity Then
                            result.Append(">")
                        ElseIf XmlNodeType.EndEntity Then
                            result.Append("/>")
                        End If
                    End If

            Case XmlNodeType.EndElement
                result.Append("</" + reader.Name + ">")

        End Select
    End While

Example sample of the XML which shows the 3 different types of tags:

    <DocumentSMG Version="6.900000" VersionSeemage="6.12.0.2428">
     <Server Type="PropertyDefinitions">
        <MetaProperties>
        </MetaProperties>
     </Server>
      <Server Type="aServerType1">
        <BOM.Sort.Ascendant Value="1"/>
     </Server>
      <Server Type="aServerType2"/>
      <Server Type="aServerType3"/>
    </DocumentSMG>

Solution

  • Try this

    Imports System.Xml
    Imports System.Xml.Linq
    Module Module1
        Const FILENAME As String = "c:\temp\test.xml"
        Sub Main()
            Dim reader As XmlReader = XmlReader.Create(FILENAME)
    
            While Not reader.EOF
                If reader.Name <> "Server" Then
                    reader.ReadToFollowing("Server")
                End If
                If Not reader.EOF Then
                    Dim server As XElement = XElement.ReadFrom(reader)
                    Dim type As String = server.Attribute("Type")
                    Select Case type
                        Case "PropertyDefinitions"
    
                        Case "aServerType1"
    
                        Case "aServerType2"
    
                        Case "aServerType3"
    
                    End Select
    
                End If
    
            End While
    
    
    
        End Sub
    
    End Module