# Reading XML using VB.Net help?



## JayJ24 (Jan 8, 2008)

I am trying with no success to read an xml file using VB. Basically would like some ideas on how you would approach reading this type of xml file



```
<oriondata station='The Gift Shop'>
<meas name="gsClerkName">Bob</meas>
<meas name="gsItem">Hat</meas>
<meas name="gsPrice">5</meas>
<meas name="gsQuanity">25</meas>
</oriondata>
```

I am learning how to read xml files and this one xml file that does not seem to be well formed is giving me trouble. The following is the code I tried:


```
Imports System.Xml

Partial Class ReadXML
    Inherits System.Web.UI.Page

    Private Sub ReadXML()

        Dim xmlFile As String = Server.MapPath("GiftShop.xml")

        Dim reader As New XmlTextReader(xmlFile)
        Dim str As New StringBuilder()
        reader.ReadStartElement("oriondata")



        While reader.Read()
            If reader.Name = "meas" And reader.NodeType = XmlNodeType.Element Then
                reader.ReadStartElement("meas")
                str.Append("<ul><b>")
            End If
End While

 reader.Close()

        lblXml.Text = str.ToString()
```

I also tried using a case statement to filter through each element but that did not work either.


Thanks


----------



## JayJ24 (Jan 8, 2008)

Okay I got it I just used the xpathNavigator method and seems to be working

lots of reading but feels great when you get it to work


----------



## Ninjaboi (Dec 27, 2010)

Glad you got it working. I do that all the time as well, state a question to someone and while I'm asking the question or soon after I solve my own problem lol.


----------



## JayJ24 (Jan 8, 2008)

Thanks for the response yea I know its on me to learn that why I usually dont like posting so much but sometimes i just get so frustrated from not understanding what I am reading that I end up deciding to post but in the mean time while im waiting for a response I still continue to try.


Now I understand a lot more than i ever did about xml files and using vb to read them. 

I was able to get the entire xml file to show up But I am not to sure how to approach getting it to format into a table using html.

Maybe you could help with shedding some light on that I would be grateful 

Thanks


heres the code:



```
Imports System.Xml
Imports System.Xml.XPath
Partial Class xmlfileread
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'have to load the xml file into memory
        Dim xmlFile As String = Server.MapPath("GiftShop.xml")

        ' Loading the XML file in an XmlDocument.
        Dim doc As New XmlDocument()
        doc.Load(xmlFile)

        Dim xnav As XPathNavigator = doc.CreateNavigator()

        'using a label to display the xml file and calling a function
        lblXml.Text = Getnav(xnav, 0)


        

    End Sub

    Private Function GetNav(ByVal xnav As XPathNavigator, ByVal level As Integer) As String

        Dim indent As String = ""
        For i As Integer = 0 To level - 1
            indent += "     "
        Next

        Dim str As New StringBuilder("")

        Select Case xnav.NodeType
            Case XPathNodeType.Root
                str.Append("<b>ROOT</b>")
                str.Append("<br />")
                Exit Select
            Case XPathNodeType.Element
                str.Append(indent)
                str.Append("Element: <b>")
                str.Append(xnav.Name)
                str.Append("</b><br />")
                Exit Select

                'grabbing values from xml file
            Case XPathNodeType.Text
                str.Append(indent)
                str.Append(" - Value: <b>")
                str.Append(xnav.Value)
                str.Append("</b><br />")
                Exit Select
            Case XPathNodeType.Comment
                str.Append(indent)
                str.Append("Comment: <b>")
                str.Append(xnav.Value)
                str.Append("</b><br />")
                Exit Select
        End Select
        If xnav.HasAttributes Then
            xnav.MoveToFirstAttribute()
            Do
                str.Append(indent)
                str.Append(" - Attribute: <b>")
                str.Append(xnav.Name)
                str.Append("</b> Value: <b>")

                str.Append(xnav.Value)
                str.Append("</b><br />")
            Loop While xnav.MoveToNextAttribute()
            ' Return to the parent.
            xnav.MoveToParent()
        End If

        If xnav.HasChildren Then
            xnav.MoveToFirstChild()
            Do
                str.Append(GetXNavDescr(xnav, level + 1))
            Loop While xnav.MoveToNext()
            ' Return to the parent.
            xnav.MoveToParent()
        End If
        Return str.ToString()

    End Function

End Class
```


----------



## Ninjaboi (Dec 27, 2010)

Perhaps these links may help:

How To Convert an HTML Table into an XML File - CodeProject

http://astahost.com/info.php/embedding-xml-html_t216.html

I'm not very good with XML files, nor do I use VB as a language, so hopefully those help you. If not, please tell me so and I'll try my best to answer the question.


----------

