Search code examples
vb.netxmlnode

What is the equivalent of FirstSibling in .NET XMLNode?


I have a dynamic number of <image> elements in an xml doc. When I reach the end of those elements I was to set the attribute on the first of these siblings to true again.

How do I get FirstSibling or something like that?

Non-Dynamic but works parentNode.PreviousSibling.PreviousSibling.PreviousSibling.Attributes(0).Value = "true"

XML:

<imageConfig>
  <image active="false">
    <pathName>/Images/123.jpg</pathName>
    <imageStartTime>Mon Dec 29 09:34:35 EST 2014</imageStartTime>
  </image>
  <image active="false">
    <pathName>/Images/1234.jpg</pathName>
    <imageStartTime>Mon Dec 29 09:34:35 EST 2014</imageStartTime>
  </image>
  <image active="false">
    <pathName>/Images/12345.jpg</pathName>
    <imageStartTime>Mon Dec 29 09:34:35 EST 2014</imageStartTime>
  </image>
  <image active="false">
    <pathName>/Images/123456.jpg</pathName>
    <imageStartTime>Mon Dec 29 09:34:35 EST 2014</imageStartTime>
  </image>
  <image active="true">
    <pathName>/Images/1234567.jpg</pathName>
    <imageStartTime>Mon Dec 29 09:34:35 EST 2014</imageStartTime>
  </image>
</imageConfig>

Solution

  • Use XPath with the preceding-sibling axis:

    Dim firstSibling As XmlNode = parentNode.SelectNodes("preceding-sibling::*")(0)
    

    See Documentation