-3

need to ask, I tried lot of options, but it still do not work properly.

I have a simple XML file which contains this:

<?xml version="1.0" encoding="UTF-8"?>
<messages>
<message>
<title>Hello</title>
<author>John</author>
<text>How are you?</text>
</message>
</messages>

And I need to show only one thing from messages. For example I make this code, but this cannot works at all, I think it will show me some objects but it takes an error.

Here is a code:

Imports System.Xml
Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim commands As XmlReader = New XmlTextReader("C:/xampp/test.xml")

    MsgBox(commands.ReadContentAsString)

End Sub
End Class

I think taht it will be some while or something, but I do not know the code way... I want to make it really simple only reading.

Thanks for help, hope I have everything well described.

Have a nice day!

Mario
  • 187
  • 2
  • 10
  • Possible duplicate of [How does one parse XML files?](http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files) – Ken White Mar 14 '17 at 00:12
  • Visual Basic 2015, bad tagging – Mario Mar 14 '17 at 00:19
  • @DavidG: The poster tagged it C#. There's a C# answer. If the poster wanted something else then he should have tagged it appropriately. It's not like it's easy to confuse C# and VB.Net. – Ken White Mar 14 '17 at 00:28

3 Answers3

0

in your xml file your tag is missing '>'. Maybe that's the problem.

If you work big xml files, you can use here to check your xml file.

 <?xml version="1.0" encoding="UTF-8"?>
<messages>
<book>
<title>Hello</title>
<author>John</author>
<text>How are you?</text**>**
</book>
</messages>
Burak Altin
  • 450
  • 7
  • 7
0

Ok, already done, this is working solution :)

   Imports System.Xml
   Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim xmlDoc As New XmlDocument()
    xmlDoc.Load("c:\xampp\test.xml")
    Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/messages/message")
    Dim title As String = "", author As String = "", text As String = ""
    For Each node As XmlNode In nodes
        title = node.SelectSingleNode("title").InnerText
        author = node.SelectSingleNode("author").InnerText
        Text = node.SelectSingleNode("text").InnerText
        MessageBox.Show(title & " " & author & " " & text)
    Next

End Sub
End Class
Mario
  • 187
  • 2
  • 10
0

You can look into XML in Visual Basic and Accessing XML in Visual Basic

Dim x = System.Xml.Linq.XDocument.Load("C:/xampp/test.xml")

MsgBox(x...<title>.Value)
Slai
  • 22,144
  • 5
  • 45
  • 53