Advertisement
PiToLoKo

Untitled

Jun 30th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.02 KB | None | 0 0
  1.  
  2. ' [ XML Writer Helper ]
  3. '
  4. ' // By Elektro H@cker
  5. '
  6. ' Example usage :
  7. '
  8. 'Private Sub Test()
  9. '
  10. '    ' Set an XML file to create
  11. '    Dim xmlfile As String = "C:\My XML File.xml"
  12. '
  13. '    ' Create the XmlWriter object
  14. '    Dim XmlWriter As Xml.XmlTextWriter = _
  15. '        New Xml.XmlTextWriter(xmlfile, System.Text.Encoding.Default) _
  16. '        With {.Formatting = Xml.Formatting.Indented}
  17. '
  18. '    ' Write the Xml declaration.
  19. '    XMLHelper.Write_Beginning(XmlWriter)
  20. '    ' Output at this point:
  21. '    ' <?xml version="1.0" encoding="Windows-1252"?>
  22. '
  23. '    ' Write a comment.
  24. '    XMLHelper.Write_Comment(XmlWriter, "XML Songs Database", Xml.Formatting.Indented)
  25. '    ' Output at this point:
  26. '    ' <!--XML Songs Database-->
  27. '
  28. '    ' Write the root element.
  29. '    XMLHelper.Write_Beginning_Root_Element(XmlWriter, "Songs", Xml.Formatting.Indented)
  30. '    ' Output at this point:
  31. '    ' <Songs>
  32. '
  33. '    ' Write the start of a song element.
  34. '    XMLHelper.Write_Beginning_Root_Element(XmlWriter, "Song", Xml.Formatting.Indented)
  35. '    ' Output at this point:
  36. '    ' <Song>
  37. '
  38. '    ' Write a song element.
  39. '    XMLHelper.Write_Elements(XmlWriter, { _
  40. '                                         {"Name", "My Song file.mp3"}, _
  41. '                                         {"Year", "2013"}, _
  42. '                                         {"Genre", "Rock"} _
  43. '                                        }, Xml.Formatting.None)        
  44. '    ' Output at this point:
  45. '    ' <Name>My Song file.mp3</Name><Year>2007</Year><Genre>Dance</Genre>
  46. '
  47. '    ' Write the end of a song element.
  48. '    XMLHelper.Write_End_Root_Element(XmlWriter, Xml.Formatting.None)
  49. '    ' Output at this point:
  50. '    ' </Song>
  51. '
  52. '    ' Write the end of the Root element.
  53. '    XMLHelper.Write_End_Root_Element(XmlWriter, Xml.Formatting.Indented)
  54. '    ' Output at this point:
  55. '    ' </Songs>
  56. '
  57. '    ' Write the xml end of file.
  58. '    XMLHelper.Write_End(XmlWriter)
  59. '
  60. '    ' Start the file and exit
  61. '    Process.Start(xmlfile) : Application.Exit()
  62. '
  63. '    ' Final output:
  64. '    '
  65. '    '<?xml version="1.0" encoding="Windows-1252"?>
  66. '    '<!--XML Songs Database-->
  67. '    '<Songs>
  68. '    '  <Song><Name>My Song file.mp3</Name><Year>2007</Year><Genre>Dance</Genre></Song>
  69. '    '</Songs>
  70. '
  71. 'End Sub
  72.  
  73. #Region " XML Helper "
  74.  
  75. Class XMLHelper
  76.  
  77.     ''' <summary>
  78.     ''' Writes the Xml beginning declaration.
  79.     ''' </summary>
  80.     Shared Sub Write_Beginning(ByVal XmlWriter As Xml.XmlTextWriter)
  81.  
  82.         Try
  83.             XmlWriter.WriteStartDocument()
  84.  
  85.         Catch ex As InvalidOperationException
  86.             Dim errormsg As String = "This is not the first write method called after the constructor. "
  87.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  88.             ' MessageBox.Show(errormsg)
  89.  
  90.         Catch ex As Exception
  91.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  92.  
  93.         End Try
  94.  
  95.     End Sub
  96.  
  97.     ''' <summary>
  98.     ''' Writes a comment.
  99.     ''' </summary>
  100.     Shared Sub Write_Comment(ByVal XmlWriter As Xml.XmlTextWriter, _
  101.                                   ByVal Comment As String, _
  102.                                   Optional ByVal Indentation As Xml.Formatting = Xml.Formatting.Indented)
  103.  
  104.         Try
  105.             XmlWriter.Formatting = Indentation
  106.             XmlWriter.WriteComment(Comment)
  107.             XmlWriter.Formatting = Not Indentation
  108.  
  109.         Catch ex As ArgumentException
  110.             Dim errormsg As String = "The text would result in a non-well formed XML document"
  111.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  112.             ' MessageBox.Show(errormsg)
  113.  
  114.         Catch ex As InvalidOperationException
  115.             Dim errormsg As String = "The ""WriteState"" property is Closed"
  116.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  117.             ' MessageBox.Show(errormsg)
  118.  
  119.         Catch ex As Exception
  120.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  121.  
  122.         End Try
  123.  
  124.     End Sub
  125.  
  126.     ''' <summary>
  127.     ''' Writes the beginning of a root element.
  128.     ''' </summary>
  129.     Shared Sub Write_Beginning_Root_Element(ByVal XmlWriter As Xml.XmlTextWriter, _
  130.                                                  ByVal Element As String, _
  131.                                                  Optional ByVal Indentation As Xml.Formatting = Xml.Formatting.Indented)
  132.  
  133.         Try
  134.             XmlWriter.Formatting = Indentation
  135.             XmlWriter.WriteStartElement(Element)
  136.             XmlWriter.Formatting = Not Indentation
  137.  
  138.         Catch ex As System.Text.EncoderFallbackException
  139.             Dim errormsg As String = "There is a character in the buffer that is a valid XML character but is not valid for the output encoding."
  140.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  141.             ' MessageBox.Show(errormsg)
  142.  
  143.         Catch ex As InvalidOperationException
  144.             Dim errormsg As String = "The XmlTextWriter is closed or An XmlTextWriter method was called before a previous asynchronous operation finished."
  145.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  146.             ' MessageBox.Show(errormsg)
  147.  
  148.         Catch ex As Exception
  149.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  150.  
  151.         End Try
  152.  
  153.     End Sub
  154.  
  155.     ''' <summary>
  156.     ''' Writes the end of a root element.
  157.     ''' </summary>
  158.     Shared Sub Write_End_Root_Element(ByVal XmlWriter As Xml.XmlTextWriter, _
  159.                                            Optional ByVal Indentation As Xml.Formatting = Xml.Formatting.Indented)
  160.  
  161.         Try
  162.             XmlWriter.Formatting = Indentation
  163.             XmlWriter.WriteEndElement()
  164.             XmlWriter.Formatting = Not Indentation
  165.  
  166.         Catch ex As System.Text.EncoderFallbackException
  167.             Dim errormsg As String = "There is a character in the buffer that is a valid XML character but is not valid for the output encoding."
  168.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  169.             ' MessageBox.Show(errormsg)
  170.  
  171.         Catch ex As InvalidOperationException
  172.             Dim errormsg As String = "The XmlTextWriter is closed or An XmlTextWriter method was called before a previous asynchronous operation finished."
  173.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  174.             ' MessageBox.Show(errormsg)
  175.  
  176.         Catch ex As Exception
  177.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  178.  
  179.         End Try
  180.  
  181.     End Sub
  182.  
  183.     ''' <summary>
  184.     ''' Writes an element.
  185.     ''' </summary>
  186.     Shared Sub Write_Element(ByVal XmlWriter As Xml.XmlTextWriter, _
  187.                                   ByVal StartElement As String, _
  188.                                   ByVal Element As String, _
  189.                                   Optional ByVal Indentation As Xml.Formatting = Xml.Formatting.Indented)
  190.  
  191.         Try
  192.             XmlWriter.Formatting = Indentation
  193.             XmlWriter.WriteStartElement(StartElement)
  194.             XmlWriter.WriteString(Element)
  195.             XmlWriter.WriteEndElement()
  196.             XmlWriter.Formatting = Not Indentation
  197.  
  198.         Catch ex As System.Text.EncoderFallbackException
  199.             Dim errormsg As String = "There is a character in the buffer that is a valid XML character but is not valid for the output encoding."
  200.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  201.             ' MessageBox.Show(errormsg)
  202.  
  203.         Catch ex As InvalidOperationException
  204.             Dim errormsg As String = "The XmlTextWriter is closed or An XmlTextWriter method was called before a previous asynchronous operation finished."
  205.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  206.             ' MessageBox.Show(errormsg)
  207.  
  208.         Catch ex As Exception
  209.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  210.  
  211.         End Try
  212.  
  213.     End Sub
  214.  
  215.     ''' <summary>
  216.     ''' Writes multiple elements.
  217.     ''' </summary>
  218.     Shared Sub Write_Elements(ByVal XmlWriter As Xml.XmlTextWriter, _
  219.                                    ByVal Elements As String(,), _
  220.                                    Optional ByVal Indentation As Xml.Formatting = Xml.Formatting.Indented)
  221.  
  222.         Try
  223.  
  224.             XmlWriter.Formatting = Indentation
  225.  
  226.             For x As Integer = 0 To Elements.GetUpperBound(0)
  227.                 XmlWriter.WriteStartElement(Elements(x, 0))
  228.                 XmlWriter.WriteString(Elements(x, 1))
  229.                 XmlWriter.WriteEndElement()
  230.             Next
  231.  
  232.             XmlWriter.Formatting = Not Indentation
  233.  
  234.         Catch ex As System.Text.EncoderFallbackException
  235.             Dim errormsg As String = "There is a character in the buffer that is a valid XML character but is not valid for the output encoding."
  236.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  237.             ' MessageBox.Show(errormsg)
  238.  
  239.         Catch ex As InvalidOperationException
  240.             Dim errormsg As String = "The XmlTextWriter is closed or An XmlTextWriter method was called before a previous asynchronous operation finished."
  241.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  242.             ' MessageBox.Show(errormsg)
  243.  
  244.         Catch ex As Exception
  245.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  246.  
  247.         End Try
  248.  
  249.     End Sub
  250.  
  251.     ''' <summary>
  252.     ''' Writes the xml end of file.
  253.     ''' </summary>
  254.     Shared Sub Write_End(ByVal XmlWriter As Xml.XmlTextWriter)
  255.  
  256.         Try
  257.             XmlWriter.WriteEndDocument()
  258.             XmlWriter.Close()
  259.  
  260.         Catch ex As ArgumentException
  261.             Dim errormsg As String = "The XML document is invalid."
  262.             Throw New Exception(errormsg & Environment.NewLine & ex.StackTrace)
  263.             ' MessageBox.Show(errormsg)
  264.  
  265.         Catch ex As Exception
  266.             Throw New Exception(ex.Message & Environment.NewLine & ex.StackTrace)
  267.  
  268.         End Try
  269.  
  270.     End Sub
  271.  
  272. End Class
  273.  
  274. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement