ASP.Net/XML/XML serialization

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

XML serialization (C#)

   <source lang="csharp">

<%@ Import Namespace="System.Runtime.Serialization" %> <%@ Import Namespace="System.Xml.Serialization" %> <%@ Import Namespace="System.Data" %> <%@ Page language="c#" %> <script language="C#" runat="server"> private catalog Catalog; private string xmlPath; private void Page_Load(object sender, System.EventArgs e) {

 xmlPath = Server.MapPath("cdcatalog.xml");
 if(Catalog==null) {
   Catalog = LoadData(xmlPath);
   foreach(catalogCD cd in Catalog.Items) {
     titleDropDownList.Items.Add(new System.Web.UI.WebControls.ListItem(cd.title));
   }
 }

} private catalog LoadData(string path) {

 try {
   System.IO.FileStream fs = System.IO.File.OpenRead(path);
   byte[] buff = new byte[fs.Length];
   fs.Read(buff, 0, (int)fs.Length);
   fs.Close();
   Catalog = (catalog)Serialization.DeSerializeXML(System.Text.ASCIIEncoding.ASCII.GetString(buff), typeof(catalog));
   return Catalog;
 } catch(Exception) {
   return null;
 }

} private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) {

 string findvalue = titleDropDownList.SelectedItem.Text;
 foreach(catalogCD cd in Catalog.Items) {
   if(cd.title==findvalue) {
     artistTextBox.Text=cd.artist;
     countryTextBox.Text=cd.country;
     companyTextBox.Text=cd.rupany;
     priceTextBox.Text=cd.price;
     yearTextBox.Text=cd.year;
     break;
   }
 }    

} private void SaveButton_Click(object sender, System.EventArgs e) {

 string findvalue = titleDropDownList.SelectedItem.Text;
 catalogCD foundcd=null;
 foreach(catalogCD cd in Catalog.Items) {
   if(cd.title==findvalue) {
     foundcd=cd;
     break;
   }
 }
 if(foundcd!=null) {
   foundcd.artist=artistTextBox.Text;
   foundcd.country=countryTextBox.Text;
   foundcd.rupany=companyTextBox.Text;
   foundcd.price=priceTextBox.Text;
   foundcd.year=yearTextBox.Text;
   System.IO.MemoryStream data = Serialization.SerializeXML(Catalog, typeof(catalog));
   byte[] databytes = data.ToArray();
   if(System.IO.File.Exists(xmlPath)) System.IO.File.Delete(xmlPath);
   System.IO.FileStream f = System.IO.File.OpenWrite(xmlPath);
   f.Write(databytes, 0, databytes.Length);
   f.Close();
 }

} [System.Xml.Serialization.XmlRootAttribute("catalog", Namespace="", IsNullable=false)] public class catalog {

   [System.Xml.Serialization.XmlElementAttribute("cd")]
   public catalogCD[] Items;

} public class catalogCD {

   public string title;    
   public string artist;    
   public string country;    
   public string company;    
   public string price;    
   public string year;

}

public class Serialization {

 public static System.IO.MemoryStream SerializeXML(object request, System.Type type) {
   try {
     System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);        
     System.IO.MemoryStream stm  = new System.IO.MemoryStream();
     serializer.Serialize(stm, request);
     return stm;
   } catch(Exception e){
     return null;
   }
 }
 public static object DeSerializeXML(string envelope, System.Type type) {
   try {
     System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);        
     System.IO.MemoryStream stm = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(envelope));
     object ud = serializer.Deserialize(stm);
     stm.Close();
     return ud;
   } catch(Exception e){
     return null;
   }
 }

} </script> <HTML>

 <HEAD>
   <title>Creating a Class from an XML Document</title>
 </HEAD>
 <body>
   <form id="Form1" method="post" runat="server">

<asp:Label id="titleLabel" runat="server">Title:</asp:Label> <asp:DropDownList id="titleDropDownList" runat="server" Width="239px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>

<asp:Label id="artistLabel" runat="server">Artist:</asp:Label> <asp:TextBox id="artistTextBox" runat="server"></asp:TextBox>

<asp:Label id="countryLabel" runat="server">Country:</asp:Label> <asp:TextBox id="countryTextBox" runat="server"></asp:TextBox>

<asp:Label id="companyLabel" runat="server">Company:</asp:Label> <asp:TextBox id="companyTextBox" runat="server"></asp:TextBox>

<asp:Label id="priceLabel" runat="server">Price:</asp:Label> <asp:TextBox id="priceTextBox" runat="server"></asp:TextBox>

<asp:Label id="yearLabel" runat="server">Year:</asp:Label> <asp:TextBox id="yearTextBox" runat="server"></asp:TextBox>

<asp:Button id="SaveButton" runat="server" Text="Save Changes" OnClick="SaveButton_Click"></asp:Button>

   </form>
 </body>

</HTML>

</source>
   
  


XML serialization (VB)

   <source lang="csharp">

<%@ 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.rupany
     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.rupany = 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">

<asp:Label id="titleLabel" runat="server">Title:</asp:Label> <asp:DropDownList id="titleDropDownList" runat="server" Width="239px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>

<asp:Label id="artistLabel" runat="server">Artist:</asp:Label> <asp:TextBox id="artistTextBox" runat="server"></asp:TextBox>

<asp:Label id="countryLabel" runat="server">Country:</asp:Label> <asp:TextBox id="countryTextBox" runat="server"></asp:TextBox>

<asp:Label id="companyLabel" runat="server">Company:</asp:Label> <asp:TextBox id="companyTextBox" runat="server"></asp:TextBox>

<asp:Label id="priceLabel" runat="server">Price:</asp:Label> <asp:TextBox id="priceTextBox" runat="server"></asp:TextBox>

<asp:Label id="yearLabel" runat="server">Year:</asp:Label> <asp:TextBox id="yearTextBox" runat="server"></asp:TextBox>

<asp:Button id="SaveButton" runat="server" Text="Save Changes" OnClick="SaveButton_Click"></asp:Button>

   </form>
 </body>

</HTML>

</source>