We help IT Professionals succeed at work.
Get Started

VB parsing thru XML

prostang
prostang asked
on
163 Views
Last Modified: 2015-04-20
I am attempting to go thru an XML document and extract values and place them in a text file.  The resulting format should be:
Name,North,East,Elevation,ATT1,ATT2,etc.
Should the ATT() have no value, I need to replace the blank with a comma (serves as placeholder essentially).

I haven't started writing the part to write it out to the file as I can't this bit of code to work.  Right now, my For Next loop is counting by 2's.  ARGH!  I pulled the framework from MSDN, so I have done some research, but this being my first time parsing thru XML, I am struggling.

This is the code(I am attaching the XML file that it should go thru to this post):

    Private Sub ReadXML(FileLocation As String, FileName As String)
        Dim output As StringBuilder = New StringBuilder()
        Dim fileContents As String
        fileContents = My.Computer.FileSystem.ReadAllText(foFile.Text)

        ' Create an XmlReader
        Using reader As XmlReader = XmlReader.Create(New StringReader(fileContents))
            Dim attName() As String = {"Name", "North", "East", "Elevation", "Code", _
                                       "Bridge_No", "C_Stab_ft", "Desc", "DiaHt_in", "Dia_in", "DiaWid_in", "Dim_ft", _
                                         "E_Stab_ft", "GrateDimFt", "HDWL_Ht_ft", "Height_ft", "LegLen_ft", "LegWid_ft", _
                                         "Length_ft", "MilePostNo", "N_Stab_ft", "NoBoxes", "Number", "OpenHt_ft", "OpenWid_ft", _
                                         "PaintPanel", "S_Stab_ft", "SigDirectn", "Spread_ft", "Stamping", "Stand_Mast", _
                                         "Type", "TypeDesc", "W_Stab_ft", "Photo"}
            'Dim attValue(30) As String

            'Check to see if CSV has been created with same name and delete if YES
            If My.Computer.FileSystem.FileExists(FileLocation & FileName & ".csv") Then _
                My.Computer.FileSystem.DeleteFile(FileLocation & FileName & ".csv")


            'Create blank CSV file with same name
            Using sw As IO.StreamWriter = New IO.StreamWriter(FileLocation & FileName & ".csv")
                'Read the XML
                While Not (reader.EOF)
                    For cCount As Integer = 0 To attName.GetUpperBound(0) Step 1
                        MsgBox(cCount)
                        reader.ReadToFollowing(attName(cCount))
                        MsgBox(attName(cCount) & " = " & reader.ReadElementContentAsString)
                        If reader.ReadElementContentAsString = "" Then
                        output.AppendLine(",")
                        Else
                        output.AppendLine(reader.ReadElementContentAsString())
                        End If
                        cCount = cCount + 1
                    Next
                    sw.WriteLine(output.ToString)
                End While
            End Using
        End Using
    End Sub

Open in new window


test.xml
Comment
Watch Question
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 18 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE