Format XML with vb.net

Good Morning! Hope all is well in your neck of the woods. I play bass for a band tonight as well as hopefully getting to catch my daughter in a volleyball game. So today’s topic is how to format xml with vb.net. When I was grabbing and displaying the XML string, it came without line breaks and such which made it difficult to read. So my solution is here. It isn’t elegant or cute but it does work. Make it a great day!


Join me on Facebook

Private Function ParseXml(ByVal xml As String) As String
        Dim builder As New System.Text.StringBuilder()
        Dim xmlString As String = xml.Replace(">", ">" & vbCrLf). _
                Replace("</", vbCrLf & "</").Replace(vbCrLf & vbCrLf, vbCrLf).Trim()
        Dim xmlStringArray As String() = xmlString.Split(vbCrLf)
        Dim indent As Integer = 0
        Dim length As Integer = 0
        Dim bracketCount As Integer = 0
        For Each line As String In xmlStringArray
            length += line.Length + 1
            If line.IndexOf("<?xml") = -1 AndAlso _
                length < xmlString.Length Then
                If line.IndexOf("<") > -1 Then bracketCount += 1
                If bracketCount > 1 Then
                    If line.IndexOf("</") > -1 Then
                        indent -= 2
                    Else
                        indent += 2
                    End If
                End If
            End If
            builder.AppendLine(Space(indent) & line.Trim())
            If line.Trim().PadRight(2).Substring(0, 2) = "</" Then _
                indent -= 2
        Next
        Return builder.ToString()
    End Function

Technorati Tags: ,,,,,,,,,,,,,,,

Advertisements
  1. #1 by nishant on August 1, 2012 - 3:25 am

    Got error at:

    builder.AppendLine(Space(indent) & line.Trim())

    • #2 by Jay on September 15, 2012 - 11:53 pm

      Just add the below if condition after the existing if and just before the AppendLine,Everything just works fine.

      If length > xmlString.Length Then
      indent += 2
      End If

      builder.AppendLine(Space(indent) & line.Trim())
      If line.Trim().PadRight(2).Substring(0, 2) = “</" Then _
      indent -= 2

  2. #3 by Jay on September 15, 2012 - 11:52 pm

    Just add the below if condition after the existing if and just before the AppendLine,Everything just works file.
    Thanks a lot to the person who wrote this code.I found just one bug and was able to fix it and the code just works fine.
    If length > xmlString.Length Then
    indent += 2
    End If

    builder.AppendLine(Space(indent) & line.Trim())
    If line.Trim().PadRight(2).Substring(0, 2) = “</" Then _
    indent -= 2

  3. #4 by Jay on September 16, 2012 - 7:36 am

    Another bug fixed:
    Add the below if condition after the above if:
    If indent < 0 Then
    indent = 2
    End If

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: