Archived Forum Post

Index of archived forum posts

Question:

.GetChild iterating over every other child?

Jan 25 '16 at 18:05

I'm working with an xml generated by a program, and doing my best to parse it as needed. Example of a portion of the xml generated is shown below:

`<FillStyle>
    <Id>626</Id>
    <ColorId>765</ColorId>
    <Offset X="0" Y="0"/>
    <Rotation>0</Rotation>
    <Skew>0</Skew>
    <FlipX>False</FlipX>
    <Scale X="1" Y="1"/>
    <ApplyObjectTransformation>True</ApplyObjectTransformation>
    <TileImage>True</TileImage>
    <AutoScale>False</AutoScale>
    <Transformation_M0>1</Transformation_M0>
    <Transformation_M1>0</Transformation_M1>
    <Transformation_M2>0</Transformation_M2>
    <Transformation_M3>1</Transformation_M3>
    <Transformation_M4>0</Transformation_M4>
    <Transformation_M5>0</Transformation_M5>
  </FillStyle>`
`<FillStyle>
    <Id>626</Id>
    <Name>0C55M100Y0K_Orange_Fill</Name>
    <Comment/>
    <ParentId>617</ParentId>
    <IndexInParent>8</IndexInParent>
    <SecurityDescriptorId>-1</SecurityDescriptorId>
    <ExternalLayout>Censored</ExternalLayout>
    <ExternalPath>
      <Item>FillStyles</Item>
      <Item>DMI_Fills</Item>
      <Item>0C55M100Y0K_Orange_Fill</Item>
    </ExternalPath>
    <Forward/>
  </FillStyle>`

I want to grab each "FillStyle" node, and combine them into one larger node, which i'm doing with the "InsertChildTreeAfter" function paired with a GetChild nested for loop.

`For i = 0 To numwithtag - 1
            Try
                If child.GetParent.GetNthChildWithTag(parent, i).HasChildWithTag("Id") Then
                    id = child.GetParent.GetNthChildWithTag(parent, i).FindChild("Id").Content
                    If id = key Then
                        If timesfound = 0 Then
                            passxml = child.GetParent.GetNthChildWithTag(parent, i)
                            timesfound = timesfound + 1
                        ElseIf timesfound = 1 Then
                            For r = 0 To child.GetParent.GetNthChildWithTag(parent, i).NumChildren - 1
                                Try
                                    If child.GetParent.GetNthChildWithTag(parent, i).GetChild(r).Tag = "Id" Then
                                    Else
                                        Dim tag As String = child.GetParent.GetNthChildWithTag(parent, i).GetXml
                                        passxml.InsertChildTreeAfter(passxml.NumChildren - 1, child.GetParent.GetNthChildWithTag(parent, i).GetChild(r))
                                    End If
                                Catch ex As Exception

                                End Try

                            Next`

My issue is with the bit

`Dim tag As String = child.GetParent.GetNthChildWithTag(parent, i).GetXml`
`passxml.InsertChildTreeAfter(passxml.NumChildren - 1, child.GetParent.GetNthChildWithTag(parent, i).GetChild(r))`

I'm using the "tag" to see which node is grabbed. For this instance, the first node, that has the nodes merged with it, is the one that contains "<name>0C55M100Y0K_Orange_Fill</name>" within it, so i'll be focusing on which nodes in the one with the "ColorId" tag are going through

For whatever reason, every other tag is going through. I understand why the "Id" tag is not going through, as it's accounted for in logic. I figured the ones with attributes were just ignored for some reason, but still doesn't account for the fact. The ones that get caught in the for loop:

Which, as I stated, is every other one. Let me know if you need more code. I'm at a lost as to why this is occuring.


Answer

I managed to fix it by using the next sibling method. it's a bit cleaner this way, so I don't fully mind. But still, is an issue. For i = 0 To numwithtag - 1 Try If child.GetParent.GetNthChildWithTag(parent, i).HasChildWithTag("Id") Then id = child.GetParent.GetNthChildWithTag(parent, i).FindChild("Id").Content If id = key Then If timesfound = 0 Then passxml = child.GetParent.GetNthChildWithTag(parent, i) timesfound = timesfound + 1 ElseIf timesfound = 1 Then Dim sibling As Chilkat.Xml = child.GetParent.GetNthChildWithTag(parent, i).FirstChild While Not sibling Is Nothing Dim check As String = sibling.Tag If sibling.Tag = "Id" Then Else passxml.NewChild(sibling.Tag, sibling.Content) End If Dim nextsibling As Chilkat.Xml = sibling.NextSibling sibling = nextsibling End While