none
XML selectNodes sometimes returning ParentNode = Nothing RRS feed

  • Question

  • Hi guys,

    I have a strange issue that I just can't get an answer.

    I have a relatively large xml to which I do the following:

        XMLDOC.Load (oFile)
        
        Set objFileNode = XMLDOC.SelectSingleNode("gfx")
        
        If Not objFileNode Is Nothing Then
            Set xmlNodeList = objFileNode.SelectNodes("group")
         .....

         end if

    As you can see, I want to catch all group nodes directly under the main node (gfx). They should all have the gfx node as parent BUT?!? when I look at each node in xmlNodeList, some of them have ParentNode = Nothing. How come? If it found it in the select line, it should know it came from it's parent or it wouldn't of found it and I need that parent value from the node later on for other stuff.

    I guess there's something I don't see or understand

    thanks

    Wednesday, February 5, 2014 10:05 PM

Answers

  • Hi,

    The above reply is just for VB.net, thanks for Cor Ligthert's reminding:)

    Here is my testing VBA code(Microsoft XML, v6.0), the xml file is the same as the above reply.

    Sub test1()
    
    Dim xmlDoc As MSXML2.DOMDocument60
    Dim xmlElement As MSXML2.IXMLDOMElement
    
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    xmlDoc.Load ("http://yoururl/testxmlfile.xml")
    Set xmlElement = xmlDoc.DocumentElement
    
    Set objFileNode = xmlElement.SelectSingleNode("gfx")
         
    If Not objFileNode Is Nothing Then
             Set xmlNodeList = objFileNode.SelectNodes("group")
             Dim Item As IXMLDOMElement
             Dim index As Integer
             index = 1
             For Each Item In xmlNodeList
             Range("A" + CStr(index)).Value = Item.nodeTypedValue
             Range("B" + CStr(index)).Value = Item.ParentNode.BaseName
             
             index = index + 1
             'MsgBox Item.ParentNode.BaseName
             Next Item
    
    End If
    
    End Sub

    The above code can insert each item's ParentNode Name to the B column:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 6, 2014 12:18 PM
  • ah man, the problem was later on in the code. I was losing my parent. Kinda weird though I didn't see it before. My cold is getting in the way I guess. thanks all anyway for your help. Really appreciated
    • Marked as answer by xilton Saturday, February 8, 2014 3:53 AM
    Saturday, February 8, 2014 3:53 AM

All replies

  • Hi guys,

    Because I don't know what the content of XML file is, so I created a test file:

    <?xml version="1.0" encoding="utf-8" ?>
    <bookstore>
      <gfx>
        <group>
          <title>Book1</title>
          <price>19.95</price>
        </group>
        <group>
          <title>Book2</title>
          <price>11.99</price>
        </group>
        <group>
          <title>Book3</title>
          <price>21.93</price>
        </group>
        <group>
          <title>Book4</title>
          <price>5.29</price>
        </group>
        <group>
          <title>Book5</title>
          <price>34.31</price>
        </group>
      </gfx>
    
      <books>
        <book>
          <title>Book6</title>
          <price>12.52</price>
        </book>
        <book>
          <title>Book7</title>
          <price>16.21</price>
        </book>
      </books>
    </bookstore>

    Here is my testing code, it adds XMLNode's information from the XmlNodeList to the ListBox(InnerText + ParentName):

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim doc As XmlDocument = New XmlDocument()
            doc.Load("..\..\testxmlfile.xml")
    
            Dim root As XmlElement = doc.DocumentElement
    
            Dim objFileNode As XmlNode = root.SelectSingleNode("gfx")
    
            If Not objFileNode Is Nothing Then
                Dim XmlNodeList = objFileNode.SelectNodes("group")
                For Each item As XmlNode In XmlNodeList
                    ListBox1.Items.Add(DirectCast(item, XmlElement).InnerText + " - ParentNode Name: " + item.ParentNode.Name)
                Next
            End If
    
        End Sub

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 6, 2014 11:13 AM
  • I don't know what program language you are using but it is 100% off topic in this forum.

    This forum is for VB versions newer then 2000 and those don't contain the keyword Set


    Success
    Cor

    Thursday, February 6, 2014 11:18 AM
  • Hi,

    The above reply is just for VB.net, thanks for Cor Ligthert's reminding:)

    Here is my testing VBA code(Microsoft XML, v6.0), the xml file is the same as the above reply.

    Sub test1()
    
    Dim xmlDoc As MSXML2.DOMDocument60
    Dim xmlElement As MSXML2.IXMLDOMElement
    
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.async = False
    xmlDoc.validateOnParse = False
    xmlDoc.Load ("http://yoururl/testxmlfile.xml")
    Set xmlElement = xmlDoc.DocumentElement
    
    Set objFileNode = xmlElement.SelectSingleNode("gfx")
         
    If Not objFileNode Is Nothing Then
             Set xmlNodeList = objFileNode.SelectNodes("group")
             Dim Item As IXMLDOMElement
             Dim index As Integer
             index = 1
             For Each Item In xmlNodeList
             Range("A" + CStr(index)).Value = Item.nodeTypedValue
             Range("B" + CStr(index)).Value = Item.ParentNode.BaseName
             
             index = index + 1
             'MsgBox Item.ParentNode.BaseName
             Next Item
    
    End If
    
    End Sub

    The above code can insert each item's ParentNode Name to the B column:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 6, 2014 12:18 PM
  • sorry,

    here's the file.

    http://crazy-xilton.com/download/821_GEN_L1_Stade1.xml

    Don't worry, it' my server so no viruses.

    For another question asked, it's VBA code in excel 2013 with reference microsoft XML 6.0  library. When I load it, the 61th item has no parent. I'm not sure if this is constant though. I have a complete zip file if needed of the code itself.

    I just noticed that the items are not in the same order they appear in the list. Looking at this group that in my list doesn'T have a parent. can't say if I have others but seems so.  <group name="grp_821_1250_LIC_All"


    • Edited by xilton Saturday, February 8, 2014 1:59 AM
    Friday, February 7, 2014 11:12 PM
  • ah man, the problem was later on in the code. I was losing my parent. Kinda weird though I didn't see it before. My cold is getting in the way I guess. thanks all anyway for your help. Really appreciated
    • Marked as answer by xilton Saturday, February 8, 2014 3:53 AM
    Saturday, February 8, 2014 3:53 AM