Help with removing column from Datatable or xml file using VB.NET

Hi,

How do I modify the code below to remove the ID column from the dataTable
or remove it from the xml file created? The ID column is included in the excel file being converted to xml.

Private Function makeDataTableFromSheetName1() As DataSet
        Try
            Dim ds As DataSet = New DataSet()
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\AOP40SC1.xls")

            conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & DestinationPath & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1"" ")
            conn.Open()

            Dim dt As New DataTable()
            adap = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", conn)
            adap.Fill(dt)
            ds.Tables.Add(dt)
            conn.Close()
            ds.Tables(0).WriteXml(Application.StartupPath + "\AOP40SC1.xml")
            Return ds
        Catch
            MsgBox("screen1" & Err.Description)
        End Try
    End Function

Open in new window



Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vcharlesAuthor Commented:
How do I modify the code below to remove a particular dataelement? The code below removes all data elements.

Dim inputXML As String = "AOP40B.XML"
        Dim saveXML As String = "AOP40B.xml"
        ''' Load the XML document
        Dim xdoc = XDocument.Load(inputXML)

        ''' Replace the "NODENAME"
        Dim results As List(Of XElement) = (From n In xdoc.Descendants("Link") _
                                             Select n).ToList()
        For Each node As XElement In results
            node.Remove()
        Next

        xdoc.Save(saveXML)

Open in new window

0
ste5anSenior DeveloperCommented:
Just remove it from DataTable.Columns before exporting it.

E.g.

Private Function makeDataTableFromSheetName1() As DataSet
        Try
            Dim ds As DataSet = New DataSet()
            Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath + "\AOP40SC1.xls")

            conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " & "data source='" & DestinationPath & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1"" ")
            conn.Open()

            Dim dt As New DataTable()
            adap = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", conn)
            adap.Fill(dt)
            ds.Tables.Add(dt)
            conn.Close()
            dt.Columns.Remove(dt.Columns["ID"])
            ds.Tables(0).WriteXml(Application.StartupPath + "\AOP40SC1.xml")
            Return ds
        Catch
            MsgBox("screen1" & Err.Description)
        End Try
    End Function

Open in new window

0
Fernando SotoRetiredCommented:
Hi Victor;

To remove all the ID's node from the XML try modifying your query as follows.

Dim results As List(Of XElement) = (From n In xdoc.Descendants("ID") _
                                             Select n).ToList() 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.