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

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » XML » XML serialization 
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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.