<?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>Quickduck &#187; Xml</title>
	<atom:link href="http://quickduck.com/blog/category/development/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://quickduck.com/blog</link>
	<description>Straight from the mind of geniuseseses....</description>
	<lastBuildDate>Wed, 30 Jun 2010 10:52:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>FirstObject XML Editor</title>
		<link>http://quickduck.com/blog/2009/08/01/firstobject-xml-editor/</link>
		<comments>http://quickduck.com/blog/2009/08/01/firstobject-xml-editor/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 13:00:02 +0000</pubDate>
		<dc:creator>Gerrod</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://quickduck.com/blog/?p=128</guid>
		<description><![CDATA[I was looking for a fast, free XML editor today, which could handle fairly large (~100MB) XML files without too much hassle. Previously I&#8217;ve used XML Marker for this; it&#8217;s fantastic, however it has a fairly clunky interface. I wish SymbolClick would release a more modern version! Anyway, today I found FirstObject XML Editor and [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking for a fast, free XML editor today, which could handle fairly large (~100MB) XML files without too much hassle. <a href="http://quickduck.com/blog/2007/11/21/application-of-the-month/">Previously</a> I&#8217;ve used <a href="http://symbolclick.com/">XML Marker</a> for this; it&#8217;s fantastic, however it has a fairly clunky interface. I wish SymbolClick would release a more modern version!</p>
<p>Anyway, today I found <a href="http://www.firstobject.com/dn_editor.htm">FirstObject XML Editor</a> and gave it a try; so far it seems great! It&#8217;s lightweight (only a single EXE file), and it&#8217;s fast. Best of all, it has a very, very simple way to format (i.e. pretty print) XML files &#8211; just press F8!</p>
<p>Highly recommended.</p>
]]></content:encoded>
			<wfw:commentRss>http://quickduck.com/blog/2009/08/01/firstobject-xml-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persisting objects into database using xml serialization</title>
		<link>http://quickduck.com/blog/2008/02/18/persisting-objects-into-database-using-xml-serialization/</link>
		<comments>http://quickduck.com/blog/2008/02/18/persisting-objects-into-database-using-xml-serialization/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 11:56:00 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://quickduck.com/blog/2008/02/18/55/</guid>
		<description><![CDATA[Recently I showed you how to pull sql query result sets into object instances using the XmlReader and deserialization. This article aims to show you the opposite process &#8211; persisting an object instance into a database table using serialization and Sql Server xml parsing. The same Channel database table and entity class will be used [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I <a href="http://quickduck.com/blog/2008/02/15/using-xmlreader-to-go-from-sql-result-set-to-a-list-of-objects/">showed</a> you how to pull sql query result sets into object instances using the XmlReader and deserialization. This article aims to show you the opposite process &#8211; persisting an object instance into a database table using serialization and Sql Server xml parsing.</p>
<p>The same Channel database table and entity class will be used for all examples.</p>
<p><img src='http://quickduck.com/blog/wp-content/uploads/2008/02/channeltable.JPG' alt='Channel Table Schema' /></p>
<pre><code>
/// Represents a media channel (e.g. web, mobile, iphone, etc.)
public class Channel    {
  private int _id;
  private string _type, _description;

  public Channel(){}

  public Channel(string type, string description)  {
    _type = type;
    _description = description;
  }

  /// Get/Set the channel identifier
  public int Id {
    get { return _id; }
    set { _id = value; }
  }

  /// Get/Set the channel type (e.g. web, mobile, iphone, etc.)
  public string Type {
    get { return _type; }
    set { _type = value; }
  }

  /// Get/Set the description of the channel.
  public string Description {
    get { return _description; }
    set { _description = value; }
  }
}
</code></pre>
<h2>The Stored Procedure</h2>
<p>The sproc takes in an sql server xml datatype parameter. This is provided via a normal string representation of the xml. Within the SPROC you prepare the document for querying, pull out the values you require and perform the insert.</p>
<p>To understand the following SPROC, you need to have have a basic understanding of:</p>
<p><strong>a)</strong> how your object instance xml serialization format looks </p>
<pre><code>
&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Channel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;Id>0&lt;/Id&gt;
  &lt;Type>web&lt;/Type&gt;
  &lt;Description>800x600 or larger browsers&lt;/Description&gt;
&lt;/Channel&gt;'
</code></pre>
<p>and </p>
<p><strong>b)</strong> an understanding of xpath and xquery for pulling the values out from the xml.</p>
<p>Check out the &#8211; <a href="http://quickduck.com/blog/sqlxml/">Resources &#8211; Sql Xml </a>- for some links that might help you out.</p>
<pre><code>
CREATE PROCEDURE [usp_PersistChannel]
	@xmldoc XML
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE
            @iDoc int,
            @type varchar(50),
            @description varchar(255)

    EXEC sp_xml_preparedocument
           @iDoc OUTPUT,
           @xmldoc,
            '<Channel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />'

    SELECT
            @Type = [Type],
            @Description = [Description]
    FROM
            OPENXML (@iDoc, '/Channel',3)
	WITH (  [Type] varchar(50) './Type',
	Description] varchar(255) './Description' )

    EXEC sp_xml_removedocument @idoc

    INSERT INTO Channel VALUES (@Type, @Description)                

    SELECT @@IDENTITY
END
</code></pre>
<h2>Data Access Code</h2>
<p>The following is a convenience method to help aid the serialization process of an object instance.</p>
<pre><code>
        /// Serialize an instance of an object to a string.
        public string Serialize(object instance)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlSerializer serializer = new XmlSerializer(instance.GetType(), null, new Type[0], null, null);
            serializer.Serialize(sw, instance);
            return sb.ToString();
        }
</code></pre>
<h2>Usage Examples:</h2>
<pre><code>

        /// Persist a Channel.
        public void Persist(Channel channel)
        {
            channel.Id = Int32.Parse(
                                Database.ExecuteScalar(
                                   Database.GetStoredProcCommand("usp_PersistChannel", Serialize(channel))
                                ).ToString()
                             );
        }
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://quickduck.com/blog/2008/02/18/persisting-objects-into-database-using-xml-serialization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
