Results 1 to 3 of 3

Thread: Remove XML Nodes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Remove XML Nodes

    Hi All,

    What is the easiest option to remove XML Nodes? I want to convert an XML document to a comma separated document?

    I would basically delete the opening nodes, and replacing the closing nodes with a comma.

    This is because I need to write a Proof Of Concept app, and quite frankly trying to make it work with a raw XML document is not my strength
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  2. #2
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Remove XML Nodes

    Personally I would check out the DaytaSet/DataTable.ReadXML method, once the data is in a datatable there's plenty of options to output to CSV

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Remove XML Nodes

    Since you didn't show the XML ...

    Code:
            Dim foo As XElement
            ' foo = XElement.Load("path here")
            ' for test use literal
            ' 5 rows, 3 cols
            foo = <root>
                      <%= From r In Enumerable.Range(1, 5)
                          Select <row>
                                     <%= From c In Enumerable.Range(1, 3)
                                         Select <col><%= r.ToString & " " & c.ToString %></col>
                                     %>
                                 </row>
                      %>
                  </root>
    
            '<root>
            '  <row>
            '    <col>1 1</col>
            '    <col>1 2</col>
            '    <col>1 3</col>
            '  </row>
            '  <row>
            '    <col>2 1</col>
            '    <col>2 2</col>
            '    <col>2 3</col>
            '  </row>
            '  <row>
            '    <col>3 1</col>
            '    <col>3 2</col>
            '    <col>3 3</col>
            '  </row>
            '  <row>
            '    <col>4 1</col>
            '    <col>4 2</col>
            '    <col>4 3</col>
            '  </row>
            '  <row>
            '    <col>5 1</col>
            '    <col>5 2</col>
            '    <col>5 3</col>
            '  </row>
            '</root>
    Convert the above to CSV

    Code:
            Dim sb As New System.Text.StringBuilder
            For Each r As XElement In foo.Elements
                For Each c As XElement In r.Elements
                    sb.Append(c.Value)
                    sb.Append(", ")
                Next
                sb.Length -= 2
                sb.AppendLine()
            Next
            Debug.WriteLine(sb.ToString)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width