locked
Add Tagnames in the middle of XML RRS feed

  • Question

  • Add Tagnames in the middle of XML

    Input

    <root> <Task> <TaskDescription>Testing</TaskDescription> <TaskType>Testing</TaskType> <AssignedToGroup> <Group> <GroupID>DDDDD</GroupID> </Group> </AssignedToGroup> <TaskAbstract>Testing</TaskAbstract> </Task> </root>

    I need write xml in real time based on loop.

    Output Required : Need to Write XML if i = 2 then Output Should be

                <root>
                                          <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                         <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                                      </root>

    If i = 3 then 3 times Task i need to Write XML

    Monday, April 25, 2016 6:54 PM

Answers

  • Hi ID GO,

    I have created a demo as below to achieve your need and you could have a look. In my demo, I use XElement to do it.

    You could first get all of the initial xml children and then add them to result as you need.

        Function GetXElements(ByVal xel As XElement) As XElement
            Dim result As XElement = Nothing
            If (xel.HasElements) Then
                For Each item In xel.Elements
                    If result Is Nothing Then
                        result = item
                    Else
                        result.Add(item)
                        If (item.HasElements) Then
                            GetXElements(item)
                        End If
                    End If
                Next
            Else
                result = xel
            End If
            Return result
        End Function
    
        Sub Main(args As String())
    
            Dim InitXML As XElement = <root>
                                          <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                                      </root>
            Dim FinalXML As XElement = Nothing
            FinalXML = InitXML
            Dim temp As XElement = GetXElements(InitXML)
    
            Console.WriteLine("Temp Result:")
            Console.WriteLine(temp)
            Console.WriteLine()
    
            'For i=2
            FinalXML.Add(temp)
            Console.WriteLine("Current FinalXML Result:")
            Console.WriteLine(FinalXML)
            Console.WriteLine()
            'For i=3
            FinalXML.Add(temp)
            Console.WriteLine("Current FinalXML Result:")
            Console.WriteLine(FinalXML)
            Console.WriteLine()
        End Sub
    

    Best Regards,

    Weibo Zhang


    • Marked as answer by ID GO Thursday, April 28, 2016 8:11 AM
    Tuesday, April 26, 2016 8:22 AM

All replies

  • Not clear what you are asking / doing.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Monday, April 25, 2016 7:21 PM
  • I'm assuming that your question is about inserting new tags between the two <root> tags.

    Without a better idea of how you generate this XML, we can only give general suggestions.

    On the surface this looks like a simple matter of using a StringBuilder to build up the XML text.  First you append <root> to the StringBuilder.  Then you start your For I = 1 to n loop, where n is the number of tasks you need to write.  Within that loop you can write out each of your task elements with their child elements.  After the loop exits you can append the closing </root> to the StringBuilder.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, April 26, 2016 2:56 AM
  • Current XML i have something like this....

    <root>
        <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                    <Group>
                                                      <GroupID>DDDDD</GroupID>
              </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
           </Task>
       </root>

    Assuming i have for loop i = 2 then I need to write xml like this...

    I need to generate a New XML...

     <root>
                                          <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                         <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                                      </root>

    Assuming i have for loop i = 3 then I need to write xml like this...

    I need to generate a New XML...

    <root>
        <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                    <Group>
                                                      <GroupID>DDDDD</GroupID>
              </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
           </Task>
    <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                    <Group>
                                                      <GroupID>DDDDD</GroupID>
              </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
           </Task>
    <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                    <Group>
                                                      <GroupID>DDDDD</GroupID>
              </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
           </Task>
       </root>


    Tuesday, April 26, 2016 3:52 AM
  • Hi ID GO,

    I have created a demo as below to achieve your need and you could have a look. In my demo, I use XElement to do it.

    You could first get all of the initial xml children and then add them to result as you need.

        Function GetXElements(ByVal xel As XElement) As XElement
            Dim result As XElement = Nothing
            If (xel.HasElements) Then
                For Each item In xel.Elements
                    If result Is Nothing Then
                        result = item
                    Else
                        result.Add(item)
                        If (item.HasElements) Then
                            GetXElements(item)
                        End If
                    End If
                Next
            Else
                result = xel
            End If
            Return result
        End Function
    
        Sub Main(args As String())
    
            Dim InitXML As XElement = <root>
                                          <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                                      </root>
            Dim FinalXML As XElement = Nothing
            FinalXML = InitXML
            Dim temp As XElement = GetXElements(InitXML)
    
            Console.WriteLine("Temp Result:")
            Console.WriteLine(temp)
            Console.WriteLine()
    
            'For i=2
            FinalXML.Add(temp)
            Console.WriteLine("Current FinalXML Result:")
            Console.WriteLine(FinalXML)
            Console.WriteLine()
            'For i=3
            FinalXML.Add(temp)
            Console.WriteLine("Current FinalXML Result:")
            Console.WriteLine(FinalXML)
            Console.WriteLine()
        End Sub
    

    Best Regards,

    Weibo Zhang


    • Marked as answer by ID GO Thursday, April 28, 2016 8:11 AM
    Tuesday, April 26, 2016 8:22 AM
  • Hi ID GO,

    I have created a demo as below to achieve your need and you could have a look. In my demo, I use XElement to do it.

    You could first get all of the initial xml children and then add them to result as you need.

        Function GetXElements(ByVal xel As XElement) As XElement
            Dim result As XElement = Nothing
            If (xel.HasElements) Then
                For Each item In xel.Elements
                    If result Is Nothing Then
                        result = item
                    Else
                        result.Add(item)
                        If (item.HasElements) Then
                            GetXElements(item)
                        End If
                    End If
                Next
            Else
                result = xel
            End If
            Return result
        End Function
    
        Sub Main(args As String())
    
            Dim InitXML As XElement = <root>
                                          <Task>
                                              <TaskDescription>Testing</TaskDescription>
                                              <TaskType>Testing</TaskType>
                                              <AssignedToGroup>
                                                  <Group>
                                                      <GroupID>DDDDD</GroupID>
                                                  </Group>
                                              </AssignedToGroup>
                                              <TaskAbstract>Testing</TaskAbstract>
                                          </Task>
                                      </root>
            Dim FinalXML As XElement = Nothing
            FinalXML = InitXML
            Dim temp As XElement = GetXElements(InitXML)
    
            Console.WriteLine("Temp Result:")
            Console.WriteLine(temp)
            Console.WriteLine()
    
            'For i=2
            FinalXML.Add(temp)
            Console.WriteLine("Current FinalXML Result:")
            Console.WriteLine(FinalXML)
            Console.WriteLine()
            'For i=3
            FinalXML.Add(temp)
            Console.WriteLine("Current FinalXML Result:")
            Console.WriteLine(FinalXML)
            Console.WriteLine()
        End Sub
    

    Best Regards,

    Weibo Zhang



    This Code Works Albert...Altered few lines as per my requirement....Thank You 
    Thursday, April 28, 2016 8:12 AM