# New Favourite Toy - Xml Serialization for generating Xml Feeds

<datetime class="hidden">2003-10-02T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported -->

## 
[New Favourite Toy - Xml Serialization for generating Xml Feeds](/blog/565) 

posted on [7:41 PM](/blog/565)

I've previously used [Xml Serialization](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserialization.asp) for loading and saving config files into and out of apps - nice use...What I've done recently though has been very useful - for me at least.
Essentially, I have an application which has a little Poll control, now in ASP.NET this is constructed by binding a collection of items (OptionItem or ResultItem depending on the 'mode')...which are in themselves contained in a VoteItem class to certain controls in the page and to a repeater for displaying the options.
Now, a project came in recently which wants to use the functionality of the Poll from ASP - what I decided to do was provide it in the form of an XML feed which the ASP could transform using XSLT into the presentation front-end. What I had planned to do was manually construct the XML string based on the VoteItem class...but then I remembered XMLSerialization - basically one step and I had an XML representation of the vote item - yay! 
Anyway, I'll modify the code in the next couple of days and upoad a version of the poll to this site (I may even have a poll on here :-)). For the moment, here's a snippet of code which will generate an XML string given a class (any class pretty much...just used it for votes in mine...) 

```
  public string SerializeVote(VoteItem theVote)
  {
   XmlSerializer xSer = new XmlSerializer(typeof(VoteItem));
   MemoryStream writer = new MemoryStream();
   xSer.Serialize(writer,theVote);
   string outStr =  System.Text.ASCIIEncoding.ASCII.GetString(writer.ToArray());
   writer.Close();
   return outStr;

  }
```

UPDATE: [This](http://www.4guysfromrolla.com/webtech/012302-1.shtml) is a prettry good article on Xml Serialization...