Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:01:31+00:00 2026-06-09T16:01:31+00:00

How can I find the sibling of an XML Element with Visual Basic? Let’s

  • 0

How can I find the sibling of an XML Element with Visual Basic? Let’s say I have:

<Data>
  <Mail>
    <Subject>Welcome!</Subject>
    <From>Antonios</From>
    <Content>Welcome! How can I assist you?</Content>
  </Mail>
  <Mail>
    <Subject>Test!</Subject>
    <From>John</From>
    <Content>Hello Friend!</Content>
  </Mail>
</Data>

Now I have a list box that adds every Subject, so the list shows: Welcome! and Test!
Now I want that, when I click on “Welcome!”, a Text Box shows the content of the “From” element of “Welcome!” and another Text Box shows the “Content” element of “Welcome”.
In other words, I’m looking for the Sibling of a specific element.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:01:32+00:00Added an answer on June 9, 2026 at 4:01 pm

    You can do this in multiple ways. Here is how you can do it using XmlDocument and XPath:

    Dim doc As New XmlDocument()
    Dim From As String = doc.SelectSingleNode("/Data/Mail[Subject='Welcome!']/From").InnerText
    Dim Content As String = doc.SelectSingleNode("/Data/Mail[Subject='Welcome!']/Content").InnerText
    

    Obviously, since the subject may not be unique, it would be better to use some element that is a unique ID, or you could do it by index. For instance, this selects from the first mail message:

    Dim From As String = doc.SelectSingleNode("/Data/Mail[1]/From").InnerText
    

    However, the best way to do something like this is to load all of the required data into memory up-front. Unless the amount of data is too great, which it doesn’t sound like it is, that usually makes the most sense. So, for instance, I would recommend creating a data object that represents a mail message, for instance:

    Public Class Mail
        Public Property Subject() As String
            Get
                Return _subject
            End Get
            Set(ByVal value As String)
                _subject = value
            End Set
        End Property
        Private _subject As String
    
        Public Property From() As String
            Get
                Return _from
            End Get
            Set(ByVal value As String)
                _from = value
            End Set
        End Property
        Private _from As String
    
        Public Property Content() As String
            Get
                Return _content
            End Get
            Set(ByVal value As String)
                _content = value
            End Set
        End Property
        Private _content As String
    
        Public Overrides Function ToString() As String
            Return _subject
        End Function
    End Class
    

    Then, you can load one Mail object for each Mail element in the XML. Because the ToString method is overriden to display the subject, you can just add the objects directly to the list box, for instance:

    Dim doc As New XmlDocument()
    For Each node As XmlNode In doc.SelectNodes("/Data/Mail")
        Dim mail As New Mail()
        mail.Subject = node.SelectSingleNode("Subject").InnerText
        mail.From = node.SelectSingleNode("From").InnerText
        mail.Content = node.SelectSingleNode("Content").InnerText
        ListBox1.Items.Add(mail)
    Next
    

    Then, when an item in the list box is selected, you can cast the selected item to the Mail type and access its properties, for instance:

    Dim mail As Mail = CType(ListBox1.SelectedItem, Mail)
    Label1.Text = mail.From
    Label2.Text = mail.Content
    

    However, at that point, if the data is not too large, it’s even easier to just use the XmlSerializer to simply deserialize the XML into an object, for instance, start by creating a class that defines the entire XML document, like this:

    Public Class Data
        <XmlElement("Mail")> _
        Public Property Mails() As List(Of Mail)
            Get
                Return _mails
            End Get
            Set(ByVal value As List(Of Mail))
                _mails = value
            End Set
        End Property
        Private _mails As List(Of Mail)
    End Class
    

    Then load the XML into the list box like this (where xml is a string containing the XML document):

    Dim serializer As New XmlSerializer(GetType(Data))
    Dim reader As New StringReader(xml)
    Dim data As Data = CType(serializer.Deserialize(reader), Data)
    ListBox1.Items.AddRange(data.Mails.ToArray())
    

    Or, if you want to read from an XML file directly instead of deserializing it from a string:

    Dim serializer As New XmlSerializer(GetType(Data))
    Using stream As New FileStream("Test.xml", FileMode.Open)
        Dim data As Data = CType(serializer.Deserialize(stream), Data)
        ListBox1.Items.AddRange(data.Mails.ToArray())
    End Using
    

    To answer your second question that you asked in your comment below, to delete a given mail message, you could do it like this:

    Dim node As XmlNode = doc.SelectSingleNode("/Data/Mail[Subject='Welcome!']")
    node.ParentNode.RemoveChild(node)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quite simple problem but i can't seem to resolve it. Let's
Lets say I have this XML: <images> <photo> <thumbImg>images/thumbs/002.jpg</thumbImg> <filename>002</filename> </photo> <photo> <thumbImg>images/thumbs/008.jpg</thumbImg> <filename>008</filename>
I have a class called 'current-menu', and I want to find its previous sibling
Given a jQuery element, how can I determine if the sibling on the right
I can find the JS and HTML code in response by alert() . But
I can find the tool command in my filesystem under: /Applications/Xcode.app/Contents/Developer/usr/bin/otool If I specify
Where can find resources about best practices for SharePoint programming? I am talking about
I can find no non-deprecated way of hiding an item in a menu bar
I can find the definition files at http://www.php.net/~helly/php/ext/spl/... but I want to extend DirectoryIterator
I can find out the error in my apps. When am trying to execute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.