I want to read Xmlfile and extract its contents to display on form in textbox using vb.net2003
How to use the XmlTextReader to read the contents of an XML document and extract the contents, if can be done....
:icon_cry:

Dani AI

Generated

This thread covers the common task of loading XML into a WinForms TextBox in VB.NET 2003. asked about XmlTextReader, pointed to documentation, showed a streaming reader example, and shared a tutorial. Below are concise, practical alternatives, code you can apply immediately, and a few pitfalls to avoid.

For small-to-medium XML where you want specific nodes, load into an XmlDocument and use XPath to pick values. Replace the XPath with whatever fits your file structure.

Dim doc As New System.Xml.XmlDocument()
doc.Load("C:\path\to\yourfile.xml")
Dim n As System.Xml.XmlNode = doc.SelectSingleNode("//Order/Customer/Name")
If n IsNot Nothing Then
    TextBox1.Text = n.InnerText
End If

If the file is large or you must stream, XmlTextReader is appropriate because it does not load the whole document. The pattern below reads elements one-by-one and appends text without keeping the full XML in memory.

Using xr As New System.Xml.XmlTextReader("C:\path\to\large.xml")
    While xr.Read()
        If xr.NodeType = System.Xml.XmlNodeType.Element AndAlso xr.Name = "Product" Then
            Dim value As String = xr.ReadElementString("Product")
            TextBox1.AppendText(value & vbCrLf)
        End If
    End While
End Using

Troubleshooting tips: 1) Watch XML namespaces — SelectSingleNode will return Nothing unless you use an XmlNamespaceManager. 2) Catch System.Xml.XmlException for malformed XML and use Try/Catch around Load/Read. 3) If reading off the UI thread, marshal updates back to the form with Invoke. 4) For table-shaped XML, DataSet.ReadXml can be faster to bind to controls. 5) VB.NET 2003 targets .NET 1.1, so LINQ to XML (XDocument) is only available if you upgrade to .NET 3.5+. These points fill gaps in the replies above and help avoid the common issues people run into when extracting values to a TextBox.

Recommended Answers

All 3 Replies

Yes, you can just read the documentation of XMLReader in MSDN
www.msdn.com

Try this with a button & text box:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim m_Settings As New XmlReaderSettings()
m_Settings.ProhibitDtd = False
m_Settings.ValidationType = ValidationType.None

Dim m_reader As XmlReader = XmlReader.Create("C:\temp\myXML.xml", m_Settings)

While m_reader.Read()
Select Case m_reader.NodeType
Case XmlNodeType.Element
Exit Select
Case XmlNodeType.Text
TextBox1.Text += m_reader.Value + vbCrLf
Case XmlNodeType.EndElement
Exit Select
End Select
End While

End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.