Advertisement
Guest User

VB.NET XML/HTML Writer

a guest
Aug 13th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.89 KB | None | 0 0
  1. Written By: Iasanator
  2. Feel Free to Use For Whatever, Enjoy!
  3.  
  4. Imports System
  5. Imports System.IO
  6. Imports System.Text
  7.  
  8. Public Class XMLWriter_
  9.  
  10.     Dim fileStream As FileStream
  11.     Dim level As New Dictionary(Of Integer, String)
  12.     Dim path As String
  13.     Dim IsNewLine As Boolean = True
  14.  
  15.     Public Sub CreateFile(ByVal p As String)
  16.         path = p
  17.         fileStream = File.Create(path)
  18.         fileStream.Dispose()
  19.  
  20.         Dim extension As String
  21.         extension = IO.Path.GetExtension(path).Replace(".", "")
  22.  
  23.         If extension = "html" Then
  24.             My.Computer.FileSystem.WriteAllText(path, "<!DOCTYPE html>" & vbCrLf, False)
  25.         ElseIf extension = "xml" Then
  26.             My.Computer.FileSystem.WriteAllText(path, "<?xml version=" & ControlChars.Quote & "1.0" & ControlChars.Quote & " encoding=" & ControlChars.Quote & "utf-8" & ControlChars.Quote & "?>" & vbCrLf, False)
  27.         Else
  28.             My.Computer.FileSystem.WriteAllText(path, "<UNKNOWN FILE TYPE; ENTER HEADER HERE>" & vbCrLf, False)
  29.         End If
  30.         level.Add(0, "base")
  31.     End Sub
  32.  
  33.     Public Overloads Sub WriteHeader(ByVal name As String, ByVal newLine As Boolean)
  34.  
  35.         TabOver()
  36.  
  37.         My.Computer.FileSystem.WriteAllText(path, "<" & name & ">", True)
  38.         level.Add(GetHighestIndex() + 1, name)
  39.  
  40.         If newLine Then
  41.             AddLine()
  42.         Else
  43.             IsNewLine = False
  44.         End If
  45.     End Sub
  46.  
  47.     Public Overloads Sub WriteHeader(ByVal name As String, ByVal var1 As String, ByVal val1 As String, ByVal newLine As Boolean)
  48.  
  49.         TabOver()
  50.  
  51.         My.Computer.FileSystem.WriteAllText(path, "<" & name & " " & var1 & "=" & ControlChars.Quote & val1 & ControlChars.Quote & ">", True)
  52.         level.Add(GetHighestIndex() + 1, name)
  53.  
  54.         If newLine Then
  55.             AddLine()
  56.         Else
  57.             IsNewLine = False
  58.         End If
  59.     End Sub
  60.  
  61.     Public Overloads Sub WriteHeader(ByVal name As String, ByVal parameters As Dictionary(Of String, String), ByVal newLine As Boolean)
  62.  
  63.         TabOver()
  64.  
  65.         My.Computer.FileSystem.WriteAllText(path, "<" & name, True)
  66.  
  67.         Dim temp As String = ""
  68.         For Each n In parameters.Keys
  69.             parameters.TryGetValue(n, temp)
  70.             My.Computer.FileSystem.WriteAllText(path, " " & n & "=" & ControlChars.Quote & temp & ControlChars.Quote, True)
  71.         Next
  72.  
  73.         My.Computer.FileSystem.WriteAllText(path, ">", True)
  74.         level.Add(GetHighestIndex() + 1, name)
  75.  
  76.         If newLine Then
  77.             AddLine()
  78.         Else
  79.             IsNewLine = False
  80.         End If
  81.     End Sub
  82.  
  83.     Public Sub WriteFooter()
  84.         Dim holder As String = ""
  85.         Dim i As Integer = 0
  86.         If IsNewLine = True Then
  87.             While i < GetHighestIndex() - 1
  88.                 My.Computer.FileSystem.WriteAllText(path, "    ", True)
  89.                 i += 1
  90.             End While
  91.         End If
  92.         level.TryGetValue(GetHighestIndex(), holder)
  93.         My.Computer.FileSystem.WriteAllText(path, "</" & holder & ">", True)
  94.         level.Remove(GetHighestIndex())
  95.         AddLine()
  96.     End Sub
  97.  
  98.     Public Sub WriteObject(ByVal name As String, ByVal parameters As Dictionary(Of String, String), ByVal newLine As Boolean, ByVal img As Boolean)
  99.  
  100.         TabOver()
  101.  
  102.         My.Computer.FileSystem.WriteAllText(path, "<" & name, True)
  103.  
  104.         Dim temp As String = ""
  105.         For Each n In parameters.Keys
  106.             parameters.TryGetValue(n, temp)
  107.             My.Computer.FileSystem.WriteAllText(path, " " & n & "=" & ControlChars.Quote & temp & ControlChars.Quote, True)
  108.         Next
  109.         If img = True Then
  110.             My.Computer.FileSystem.WriteAllText(path, ">", True)
  111.         Else
  112.             My.Computer.FileSystem.WriteAllText(path, "/>", True)
  113.         End If
  114.  
  115.         If IsNewLine = True Then
  116.             AddLine()
  117.         End If
  118.     End Sub
  119.  
  120.     Public Sub WriteText(ByVal text As String)
  121.  
  122.         TabOver()
  123.  
  124.         My.Computer.FileSystem.WriteAllText(path, text, True)
  125.         If IsNewLine = True Then
  126.             AddLine()
  127.         End If
  128.     End Sub
  129.  
  130.     Public Sub EndFile()
  131.         For Each n In level.Keys
  132.             If n > 0 Then
  133.                 WriteFooter()
  134.             End If
  135.         Next
  136.     End Sub
  137.  
  138.     Private Sub AddLine()
  139.         My.Computer.FileSystem.WriteAllText(path, vbCrLf, True)
  140.         IsNewLine = True
  141.     End Sub
  142.  
  143.     Private Sub TabOver()
  144.         Dim i As Integer = 0
  145.         If IsNewLine = True Then
  146.             While i < GetHighestIndex()
  147.                 My.Computer.FileSystem.WriteAllText(path, "    ", True)
  148.                 i += 1
  149.             End While
  150.         End If
  151.     End Sub
  152.  
  153.     Private Function GetHighestIndex() As Integer
  154.         Dim i As Integer = 0
  155.  
  156.         For Each n In level.Keys
  157.             If i < n Then
  158.                 i = n
  159.             End If
  160.         Next
  161.         Return i
  162.     End Function
  163.  
  164. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement