XML serialization (C#) : XML serialization « XML « ASP.Net






XML serialization (C#)


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

 








Related examples in the same category

1.XML serialization (VB)