Mega Code Archive

 
Categories / ASP.Net / XML
 

XML serialization (VB)

<%@ Page language="vb" %> <%@ Import Namespace="System.Data" %> <script language="vb" runat="server"> Dim xmlPath As String Dim cat As catalog Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)   xmlPath = Server.MapPath("cdcatalog.xml")   If cat Is Nothing Then     cat = LoadData(xmlPath)     Dim cd As catalogCD     For Each cd In cat.Items       titleDropDownList.Items.Add(New System.Web.UI.WebControls.ListItem(cd.title))     Next cd   End If End Sub Private Function LoadData(ByVal path As String) As catalog   Try     Dim fs As System.IO.FileStream = System.IO.File.OpenRead(path)     Dim buff(fs.Length) As Byte     fs.Read(buff, 0, CInt(fs.Length))     fs.Close()     cat = CType(Serialization.DeSerializeXML(System.Text.ASCIIEncoding.ASCII.GetString(buff), GetType(catalog)), catalog)     Return cat   Catch   End Try End Function  'LoadData Private Sub titleDropDownList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)   Dim findvalue As String = titleDropDownList.SelectedItem.Text   Dim cd As catalogCD   For Each cd In cat.Items     If cd.title = findvalue Then       artistTextBox.Text = cd.artist       countryTextBox.Text = cd.country       companyTextBox.Text = cd.company       priceTextBox.Text = cd.price       yearTextBox.Text = cd.year       Exit For     End If   Next cd End Sub Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)   Dim findvalue As String = titleDropDownList.SelectedItem.Text   Dim foundcd As catalogCD = Nothing   Dim cd As catalogCD   For Each cd In cat.Items     If cd.title = findvalue Then       foundcd = cd       Exit For     End If   Next cd   If Not (foundcd Is Nothing) Then     foundcd.artist = artistTextBox.Text     foundcd.country = countryTextBox.Text     foundcd.company = companyTextBox.Text     foundcd.price = priceTextBox.Text     foundcd.year = yearTextBox.Text     Dim data As System.IO.MemoryStream = Serialization.SerializeXML(cat, GetType(catalog))     Dim databytes As Byte() = data.ToArray()     If System.IO.File.Exists(xmlPath) Then       System.IO.File.Delete(xmlPath)     End If     Dim f As System.IO.FileStream = System.IO.File.OpenWrite(xmlPath)     f.Write(databytes, 0, databytes.Length)     f.Close()   End If End Sub Public Class Serialization   Public Shared Function SerializeXML(ByVal request As Object, ByVal type As System.Type) As System.IO.MemoryStream     Try       Dim serializer As New System.Xml.Serialization.XmlSerializer(type)       Dim stm As New System.IO.MemoryStream()       serializer.Serialize(stm, request)       Return stm     Catch e As Exception       Return Nothing     End Try   End Function    'SerializeXML   Public Shared Function DeSerializeXML(ByVal envelope As String, ByVal type As System.Type) As Object     Try       Dim serializer As New System.Xml.Serialization.XmlSerializer(type)       Dim stm As New System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(envelope))       Dim ud As Object = serializer.Deserialize(stm)       stm.Close()       Return ud     Catch e As Exception       Return Nothing     End Try   End Function    'DeSerializeXML End Class  'Serialization <System.Xml.Serialization.XmlRootAttribute("catalog", [Namespace]:="", IsNullable:=False)> _ Public Class catalog   <System.Xml.Serialization.XmlElementAttribute("cd")> _   Public Items() As catalogCD End Class Public Class catalogCD   Public title As String   Public artist As String   Public country As String   Public company As String   Public price As String   Public year As String End Class </script> <HTML>   <HEAD>     <title>Creating a Class from an XML Document</title>   </HEAD>   <body>     <form id="Form1" method="post" runat="server">       <P>         <asp:Label id="titleLabel" runat="server">Title:</asp:Label>         <asp:DropDownList id="titleDropDownList" runat="server" Width="239px" AutoPostBack="True"          OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList></P>       <P>         <asp:Label id="artistLabel" runat="server">Artist:</asp:Label>         <asp:TextBox id="artistTextBox" runat="server"></asp:TextBox></P>       <P>         <asp:Label id="countryLabel" runat="server">Country:</asp:Label>         <asp:TextBox id="countryTextBox" runat="server"></asp:TextBox></P>       <P>         <asp:Label id="companyLabel" runat="server">Company:</asp:Label>         <asp:TextBox id="companyTextBox" runat="server"></asp:TextBox></P>       <P>         <asp:Label id="priceLabel" runat="server">Price:</asp:Label>         <asp:TextBox id="priceTextBox" runat="server"></asp:TextBox></P>       <P>         <asp:Label id="yearLabel" runat="server">Year:</asp:Label>         <asp:TextBox id="yearTextBox" runat="server"></asp:TextBox></P>       <P>         <asp:Button id="SaveButton" runat="server" Text="Save Changes" OnClick="SaveButton_Click"></asp:Button></P>     </form>   </body> </HTML>