<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nPath Ltd. &#187; XML</title>
	<atom:link href="http://www.npathweb.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.npathweb.com</link>
	<description>Web Development Blog</description>
	<lastBuildDate>Tue, 10 Jan 2012 02:22:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Serializing and de-serializing a listbox to XML</title>
		<link>http://www.npathweb.com/2009/02/05/serializing-and-de-serializing-a-listbox-to-xml/</link>
		<comments>http://www.npathweb.com/2009/02/05/serializing-and-de-serializing-a-listbox-to-xml/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 17:07:42 +0000</pubDate>
		<dc:creator>B.Harding</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[SSL]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.npathweb.com/blog/?p=26</guid>
		<description><![CDATA[In this post I will discuss how I stored a list of strings in a single database field as xml. The application I&#8217;m working on has a list of email addresses for each contact. Rather than creating a new table with forgein keys, stored procedures, Data access componants, and business layer logic I decided to take a regular email [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will discuss how I stored a list of strings in a single database field as xml.</p>
<p>The application I&#8217;m working on has a list of email addresses for each contact. Rather than creating a new table with forgein keys, stored procedures, Data access componants, and business layer logic I decided to take a regular email field and expand it to store a list of emails. My Datastore is SQL Server. I only changed the field to nvarchar(max).</p>
<p>To save the list</p>
<ol>
<li>edit the list as needed</li>
<li>pull the listbox.items collection and convert to an array</li>
<li>Serialize the array and store the text in the database</li>
</ol>
<p>To get it back</p>
<ol>
<li>read the text from the database</li>
<li>de-serialize to an array</li>
<li>load the array as items in the listbox</li>
</ol>
<p>The issue I had was that the xmlserializer works with streams. This I was not used to. I needed to convert from string to stream and backagain. Looking back it isn&#8217;t hard but for me it was a learning curve.</p>
<p>Here is the code then pulls the listbox and converts the items to an XML string:</p>
<pre><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">

      </span></pre>
<pre><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">        <span style="color: #006400;">'create and populate an array, the items collection is not serializable</span>
         Dim</span> emails <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> ArrayList()</span></pre>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"> For</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Each</span> itm <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> ListItem <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">In</span> ListBox1.Items<br />
emails.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Add</span>(itm.Value)<br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Next</span></p>
<p><span style="color: #006400;">&#8216;create an XmlSerializer and use it to populate a memory stream<br />
</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> xs <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> System.Xml.Serialization.XmlSerializer(<span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">GetType</span>(ArrayList))<br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> ms <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> System.IO.MemoryStream<br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Try</span><br />
xs.Serialize(ms, emails)<br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Catch</span> ex <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> Exception<br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">End</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Try<br />
</span><span style="color: #006400;"><br />
</span> <span style="color: #006400;"> &#8216;rewind the stream</span><br />
ms.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Seek</span>(0, 0)</p>
<p><span style="color: #006400;"> &#8216;read the stream to string, I use a StreamReader to take advantage of ReadToEnd</span><br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> sr <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> System.IO.StreamReader(ms)<br />
<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"> Dim</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"><span style="color: #000000;">str</span></span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">String</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> sr.ReadToEnd</p>
<p><span style="color: #006400;"> &#8216;now we can save the string to a database</span></p>
<p><span style="font-family: Arial;"><span style="font-size: x-small;"><span style="color: #000000;">Here is the code that re-populates the list from an XML string</span><br />
</span></span><br />
&#8216;we&#8217;ll start with a string of xml called <span style="color: #ff0000;">str</span>. I&#8217;ll assume it&#8217;s already pulled from the database</p>
<pre><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="color: #006400;">       'To convert a string to a stream  we have to convert to a byte array first.
       'aStr is str converted to an array of bytes</span>
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> uniEncoding <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> UnicodeEncoding()
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> aStr <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Byte</span>() <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> uniEncoding.GetBytes(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">str</span>)

<span style="color: #006400;">      'wrtie the byte array to a stream and rewind it</span>

        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> ms <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> System.IO.MemoryStream
        ms.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Write</span>(aStr, 0, aStr.Length)
        ms.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Seek</span>(0, 0)

<span style="color: #006400;">      'de-serialize from xml to an array of strings
</span>        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> emails <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> ArrayList()
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Dim</span> xs <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">New</span> System.Xml.Serialization.XmlSerializer(<span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">GetType</span>(ArrayList))

        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Try</span>
            emails <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> xs.Deserialize(ms)
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Catch</span> ex <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> Exception
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">End</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Try</span>

<span style="color: #006400;">      'load the array into the listbox's items collection</span>
        ListBox1.Items.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Clear</span>()
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">For</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Each</span> itm <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">As</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Object</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">In</span> emails
            ListBox1.Items.<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Add</span>(itm.ToString)
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">Next</span></span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.npathweb.com/2009/02/05/serializing-and-de-serializing-a-listbox-to-xml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

