This article demonstrate the basic idea of using Web Services to serialize objects and write them into XML file and deserialize data stored in XML file into objects. The example used is a typical Books and Book collection xml structure as below.

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <Books>
   3:    <Book>
   4:      <Title>The Shining</Title>
   5:      <Author>Steven King</Author>
   6:    </Book>
   7:    <Book>
   8:      <Title>Harry Potter</Title>
   9:      <Author>J.K. Roling</Author>
  10:    </Book>
  11:    <Book>
  12:      <Title>The Lord of The Rings</Title>
  13:      <Author>J. R. R. Tolkien</Author>
  14:    </Book>
  15:  </Books>

 

The root element <Books> being the collection of many <Book> items and each <Book> item has 2 sub elements <Title> and <Author> for that book. To construct such data structure in Web Services using C#, we can do the following.

   1:  [XmlRoot("Books")]
   2:      public class Books
   3:      {
   4:         private ArrayList bookList;
   5:   
   6:          public Books()
   7:          {
   8:              bookList = new ArrayList();
   9:          }
  10:   
  11:          [XmlElement("Book")]
  12:          public Book[] Items
  13:          {
  14:              get {
  15:                  Book[] books = new Book[bookList.Count];
  16:                  bookList.CopyTo(books);
  17:                  return books;
  18:              }
  19:   
  20:              set {
  21:                  if (value == null) return;
  22:                  Book[] books = (Book[])value;
  23:                  bookList.Clear();
  24:                  foreach (Book book in books)
  25:                  {
  26:                      bookList.Add(book);
  27:                  }
  28:              }
  29:          }
  30:   
  31:          public void Add(string title, string name)
  32:          {
  33:              Book book = new Book(title, name);
  34:              bookList.Add(book);
  35:          }
  36:   
  37:      }
  38:   
  39:      public class Book
  40:      {
  41:          [XmlElement("Title")] public string title;
  42:          [XmlElement("Author")] public string author;
  43:   
  44:          public Book()
  45:          {
  46:          }
  47:   
  48:          public Book(string title, string author)
  49:          {
  50:              this.title = title;
  51:              this.author = author;
  52:          }
  53:      }

 

Once the above data structure has been defined, the rest are relatively easy. XMLSerializer is the class that will be used to serializes and deserializes objects into and from XML documents. StreamReader and StreamWriter are the classes responsible to read and write XML files.

   1:  namespace XMLWebService1
   2:  {
   3:      [WebService(Namespace = "http://moss/")]
   4:      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   5:      [System.ComponentModel.ToolboxItem(false)]
   6:      public class Service1 : System.Web.Services.WebService
   7:      {
   8:          public string path = @"C:\XMLFile1.xml";
   9:   
  10:          [WebMethod]
  11:          public void Deserialize()
  12:          {
  13:              XmlSerializer xs= new XmlSerializer(typeof(Books));
  14:              StreamReader sr = new StreamReader(path);
  15:              Books bk = (Books)xs.Deserialize(sr);
  16:              sr.Close();
  17:              Book[] books = bk.Items;
  18:          }
  19:   
  20:          [WebMethod]
  21:          public void Serialize()
  22:          {            
  23:              Book[] bookArray = new Book[3];
  24:              bookArray[0] = new Book("The Shining", "Steven King");
  25:              bookArray[1] = new Book("Harry Potter", "J.K. Rolings");
  26:              bookArray[2] = new Book("The Lord of The Ring", "J. R. R. Tolkien");
  27:   
  28:              Books books = new Books();
  29:              books.Items = bookArray; 
  30:   
  31:              XmlSerializer xs = new XmlSerializer(typeof(Books));
  32:              TextWriter tw = new StreamWriter(path);
  33:              xs.Serialize(tw, books);
  34:              tw.Close();
  35:          }
  36:      }
  37:  }