XML to CSV File

  • Thread starter Thread starter Broggy69
  • Start date Start date
B

Broggy69

Guest
When I run my code below, I get an error that says:

An unhandled exception of type System.Xml.XmlException occurred in System.Xml.dll

Additional information: Data at the root level is invalid. Line 1, position 1.

Any help would be appriciated

Below is VB Code

Imports System.IO
Imports System.Text

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim xmlString As String = "C:\Users\gbowlsby\Desktop\XML2MDB\Jobs.xml"

First read your XML string in
Dim doc As XDocument = XDocument.Parse(xmlString)

Create a string builder to hold the output CSV
Dim theOutput As New StringBuilder(1000)

Loop through the nodes of the XML
For Each node As XElement In doc.Descendants("JobID")
For Each innerNode As XElement In node.Elements()
theOutput.AppendFormat("{0},", innerNode.Attribute("data").Value)
Next

Remove trailing comma
theOutput.Remove(theOutput.Length - 1, 1)
theOutput.AppendLine()
Next

Write output to file and do whatever you want the file here
File.WriteAllText("C:\Users\gbowlsby\Desktop\XML2MDB\Jobs.csv", theOutput.ToString())

End Sub



Below is first record in XML File

<?xml version="1.0"?>

-<ArrayOfRecord xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


-<Record>

<JobID>Glenn-X123</JobID>


-<RecordDetails>


-<ControlData>

<ControlName>cDTP_1</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>Date Format Day, m dd, yyyy Wednesday May 6, 2015</ControlData>

</ControlData>


-<ControlData>

<ControlName>T1</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData/>

</ControlData>


-<ControlData>

<ControlName>FTP_YN</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>RP2</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>RP1</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>txtDTP_1</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>Friday Jul 31, 2015</ControlData>

</ControlData>


-<ControlData>

<ControlName>aeBox</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>eMail</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData/>

</ControlData>


-<ControlData>

<ControlName>eBox</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>SplitYN</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>OPP</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>T_CSV</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>TXT</ControlData>

</ControlData>


-<ControlData>

<ControlName>JName</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>7320EN</ControlData>

</ControlData>


-<ControlData>

<ControlName>ALst</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>OEBB</ControlData>

</ControlData>


-<ControlData>

<ControlName>tWFldr</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>\\pw_prod\Watch_Input\OEBB\7320EN</ControlData>

</ControlData>


-<ControlData>

<ControlName>WYN</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>No</ControlData>

</ControlData>


-<ControlData>

<ControlName>BOpt</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>GBowlsby</ControlData>

</ControlData>


-<ControlData>

<ControlName>xProg</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>Logged on Computer: D-4190F3D Logged in as User : GBowlsby OS Name: Microsoft Windows 7 Professional OS Platform: Win32NT OS Version: 6.1.7601.65536 Is 64 Bit OS: True July 29 2015 / 15:19:51_ Choose not to send to Watch Update Planet Press with new artwork as needed. Date format selected Date Format Day, m dd, yyyy Wednesday May 6, 2015 Date format selected July 29 2015 / 15:20:09_ Watch Folder Selected July 29 2015 / 15:20:09_ Watch Folder Selected July 29 2015 / 15:20:09_ Job Folder Selected July 29 2015 / 15:20:12_ Watch Folder Selected Date Format Day, m dd, yyyy Wednesday May 6, 2015 Date format selected </ControlData>

</ControlData>


-<ControlData>

<ControlName>Authorized_Only</ControlName>

<ControlProperty>Text</ControlProperty>

<ControlData>This program is for authorized personal only. Only use with management authorization Any changes, modification, or deletions are prohibited without prior authorization from managers</ControlData>

</ControlData>

</RecordDetails>

</Record>


-<Record>

Continue reading...
 
Back
Top