Sunday, October 30, 2011

XML Manipulation using .NET (C#) PART I

Hello folks,
In this article I'm going to discuss how to
1. Create an XML file
2. Modify an XML elements using XPath
3. Bind an XML document to Data Grid View
4. Delete an XML node using Xpath

In the Part II, I will discuss how to
1. Search XML elements and display searched result in Data Grid View using LINQ.

Please note: Please use Firefox or IE to read this article.Sorry for the inconvenience caused.

I'm gonna create and use following XML file throughout this article.


  
    Harry Potter
    J K. Rowling
    2005
    29.99
    CHILDREN
  
  
    Everyday Italian
    Giada De Laurentiis
    2005
    30.00
    COOKING
  
  
    XQuery Kick Start
    James McGovern
    2010
    49.99
    WEB
  

First of all let's define the file path of our XML file in the web config file and create a class called XMLManager to get the file path.
The reason for this step is to keep the file path in a specific place and access it from anywhere from the application.
Insert the following code under configuration section in web config file.

    
  
The XML file we are going to create is bookDetails.xml and it locates in a folder called XML.
Here's the XMLManager class. In here I use a static method because in order to use the method we don't have to create an instance of the class.
public class XMLManager
    {
        public static string GetFilePath()
        {
            return ConfigurationManager.AppSettings["XmlFilePath"].ToString();
        }
    }


Create an XML Document
Following code snippet shows how to create above XML file using XMLDocument class.
In order to use below code you have import System.XML base class
private void writeXML()
        {
            // Create new XML document
            XmlDocument doc = new XmlDocument();

            // Create nodes and attributes
            XmlNode bookstore, book, title, author, year, price, category;
            XmlAttribute id;

            //  the root element
            bookstore = doc.CreateElement("bookstore");
            doc.AppendChild(bookstore);

            //  element with category attribute
            book = doc.CreateElement("book");            
            bookstore.AppendChild(book);
            id = doc.CreateAttribute("id");
            id.Value = getNewBookID().ToString();
            book.Attributes.Append(id);

            //  element
            title = doc.CreateElement("title");
            title.InnerText = txtTitle.Text.Trim();
            book.AppendChild(title);           

            // <author> element
            author = doc.CreateElement("author");
            author.InnerText = txtAuthor.Text.Trim();
            book.AppendChild(author);

            // <year> element
            year = doc.CreateElement("year");
            year.InnerText = txtYear.Text.Trim();
            book.AppendChild(year);

            // <price> element
            price = doc.CreateElement("price");
            price.InnerText = txtPrice.Text.Trim();
            book.AppendChild(price);

            // <category>
            category = doc.CreateElement("category");
            category.InnerText = drpCategory.SelectedItem.Text;
            book.AppendChild(category);

            // Save the XML document to XML folder
            doc.Save(Server.MapPath(XMLManager.GetFilePath()));
        }
</pre>

First create an XML document called <b>doc</b> by using <b> XMLDocument </b> class and then define necessary XML nodes and attributes. <br />
In here I have 7 XML nodes (elements) and 1 attribute called ID to store book ID.  <br />

After that create the XML root element called bookstore and append it to the XML document <b>doc</b>. Then create the <b>book</b> element with <b>ID</b> attribute and append it as a child of root element. Please note the value of the book id receive from a private method called <b>getNewBookID</b> and code for this method can be found latter part of the article. <br />

Then create the other child elements of the <b>book</b> element and append them to the <b>book</b> element. 

After creating the XML document it can be saved by using <b>Save</b> method. In here I have consumed <b>GetFilePath</b> static method of our <b> XMLManager </b> class to give the file path. The new file will be save to the location that we have defined in the web.config file. In this case XML file will be create inside the XML folder with the name <b>bookDetails</b>.

<br /><br />
Please note: You won't be able to see the created XML file in the specified location because it is not included to the project. In order to view the XML file using VS, first you have to select "Show All Files" option in the solution explorer and right click on the created bookDetails XML file (which is now exclude from the project) and select include to project option. Then and only you can see the created XML document using VS.
<br /><br />

The above code will create and save your first book details in to a XML file called "bookDetails.xml". However when you want to add another book details to an existing XML file you have to use slightly different code. Below code shows how to append another book node to above bookDetails.xml document. 
 
<pre class="brush: csharp">
private void modifyXML()
        {
            // load the existing XML document
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(XMLManager.GetFilePath()));

            XmlNode book, title, author, year, price, category;
            XmlAttribute id, lang;

            // <book> element with category attribute
            book = doc.CreateElement("book");
            id = doc.CreateAttribute("id");
            id.Value = getNewBookID().ToString();
            book.Attributes.Append(id);

            // <title> element
            title = doc.CreateElement("title");
            title.InnerText = txtTitle.Text.Trim();
            book.AppendChild(title);
            
            // <author> element
            author = doc.CreateElement("author");
            author.InnerText = txtAuthor.Text.Trim();
            book.AppendChild(author);

            // <year> element
            year = doc.CreateElement("year");
            year.InnerText = txtYear.Text.Trim();
            book.AppendChild(year);

            // <price> element
            price = doc.CreateElement("price");
            price.InnerText = txtPrice.Text.Trim();
            book.AppendChild(price);

            // <category> element
            category = doc.CreateElement("category");
            category.InnerText = drpCategory.SelectedItem.Text;
            book.AppendChild(category);

            XmlNode lastNode = doc.SelectSingleNode("bookstore/book[last()]");
            lastNode.ParentNode.InsertAfter(book,lastNode);

            doc.Save(Server.MapPath(XMLManager.GetFilePath()));
        }
</pre>

First load the existing file into a XML documents and then create <b>book</b> element with required attributes and child elements. After that find the last book node in the XML file by using XPath and insert the new book node after the last node. Then save the updated XML document by using <b>Save</b> method.

<br /><br />
The following code shows <b>getNewBookID</b> method which returns the new ID for a book.

<pre class="brush: csharp">
private int getNewBookID()
        {
            int result = 0;
            if (File.Exists(Server.MapPath(XMLManager.GetFilePath())))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath(XMLManager.GetFilePath()));
                XmlNode node = doc.SelectSingleNode("bookstore/book[last()]");
                int currentNode = Convert.ToInt32(node.Attributes["id"].Value);
                result = currentNode + 1;
            }
            else
                result = 1;

            return result;
        }
</pre>

First the application checks for the file. If it does not exists then simply return the value 1 because we will create a new book with the ID 1. If the file exists, then load the existing file to new XML file using <b>Load</b> method. Then get the last XML node of the file using XPath. you can use either <b>bookstore/book[last()]</b>or <b>//book[last()]</b> to select the last book node. More XPath syntax can be found from <a href="http://www.w3schools.com/xpath/xpath_syntax.asp" target="_blank">Here</a>. <br />

Then get the value of the ID attribute of the last node and increase it by one and return the result as new book ID. 

<br /><br />
<b>View XML Data in a Gridview Control</b> <br />
First add a Gridview control to the HTML page and add bound fields to represent the data. In here I have added 5 bound columns to represent book Id, Author, Year, Price and category. Value of the <b>DataField</b> column should be equal to names of your XML file nodes and attributes. 

<pre class="brush: csharp">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="id" 
                    onrowdeleting="GridView1_RowDeleting" 
                    onrowupdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:BoundField HeaderText="ID" DataField="id" />
                        <asp:BoundField HeaderText="Author" DataField="author" />
                        <asp:BoundField HeaderText="Year" DataField="year" />
                        <asp:BoundField HeaderText="Price" DataField="price" />                        
                        <asp:BoundField HeaderText="Category" DataField="category" />
                        <asp:ButtonField ButtonType="Link" CommandName="delete" Text="delete" />   
                        <asp:ButtonField ButtonType="Link" CommandName="update" Text="edit" />                     
                    </Columns>
                </asp:GridView>
</pre>

Apart from the bound fields I have added two button fields to delete and update Gridview rows. The command name <b>delete</b> will fire the <b>onrowdeleting</b> event and command name <b>update</b> will fire the <b>onrowupdating</b> event. Also please note that the <b> DataKeyNames</b> property has set to <b>ID</b> attribute of the book element which is use when updating and deleting an existing node. <br />

Following code shows how to bind the created XML file to gridview control using <b>ReadXml</b> method of a DataSet object. 

<pre class="brush: csharp">
        private void bindDataToGrid()
        {
            if (File.Exists(Server.MapPath(XMLManager.GetFilePath())))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath(XMLManager.GetFilePath()));
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
                lblError.Text = "File does not exists";
        }
</pre>

<br /><br />
<b>Data Grid View with XML Data</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSjMjs8lxrtVErCiiHZFwbbfE_0CzzfH_FE-hETed7JZAxcVDo9zLU7UnHHEOr8kvexibPdjDTkZ_ocDqCg4clG3jUYv0h7KPZuATB_JqVjZitBgOw2yNcxziKsFpRWxgx18Wv5pxkwbTA/s1600/gridview_xml.JPG" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="104" width="375" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSjMjs8lxrtVErCiiHZFwbbfE_0CzzfH_FE-hETed7JZAxcVDo9zLU7UnHHEOr8kvexibPdjDTkZ_ocDqCg4clG3jUYv0h7KPZuATB_JqVjZitBgOw2yNcxziKsFpRWxgx18Wv5pxkwbTA/s400/gridview_xml.JPG" /></a></div>
<br /><br />

<br /><br />
<b>Delete an XML Node  </b> <br />
When user clicks on the delete link the application will fire the <b>GridView1_RowDeleting</b> event. The following code sample shows how to remove a book node from an XML document by using book ID attribute.

<pre class="brush: csharp">
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            if (File.Exists(Server.MapPath(XMLManager.GetFilePath())))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath(XMLManager.GetFilePath()));
                XmlNode node = doc.SelectSingleNode("bookstore/book[@id='" + index.ToString() + "']");
                node.ParentNode.RemoveChild(node);

                doc.Save(Server.MapPath(XMLManager.GetFilePath()));

                bindDataToGrid();
            }
        }
</pre>

First get the ID of the book that wants to remove by using gridview dataKeys property. Then load the XML file to a new XML document and find the appropriate node by using XPath. Then you can use the RemoveChild method to remove that note from the XML document. Then use the Save method to save updated XML document to the disc. Please note you have to bindDateToGrid method in order to view the updated data grid view. 

<br/ ><br />
<b>Update an Existing XML Node </b> <br />
First I will show you how to select an existing book from the above data grid view and redirect it to another page using query string. Then I will display the selected book in edit mode where users can change the existing data and can update the book. <br />

The following code demonstrate how to select a book from the gridview. 

<pre class="brush: csharp">
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            Response.Redirect(string.Format("XML.aspx?id={0}", index.ToString()));
        }
</pre>

When user clicks on the Edit link in the data grid view, the RowUpdating event will fire and we can find the selected book ID using DataKeys property. Then we redirect the use to another page to display the selected book details where user can edit them and update that details. 

<br/ ><br />
<b>Read a Node from XML file </b> <br />
The following code shows how to read a particular book node from the XML file and show the details in the web page. 

<pre class="brush: csharp">
private void loadXML(int id)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(XMLManager.GetFilePath()));

            XmlNode node = doc.SelectSingleNode(string.Format("bookstore/book[@id='{0}']",id));
            if (node != null)
            {
                txtTitle.Text = node["title"].InnerText;
                txtAuthor.Text = node["author"].InnerText;
                txtYear.Text = node["year"].InnerText;
                txtPrice.Text = node["price"].InnerText;
                drpCategory.Items.FindByText(node["category"].InnerText).Selected=true;
                hdnID.Value = node.Attributes["id"].Value;
            }
        }
</pre>

loadXML method requires book Id as a parameter. First load the XML file and then search the XML node where id equals to the parameter. I have used the XPath to select the required node. Then I have bind the values of child nodes and values of attributes to page controls. Please not I have a hidden field called hdnID to store the book ID because I need that value when updating the record back to the XML file. 

<br/ ><br />
Now the following code shows how to save the above node with modified values to the XML document. 

<pre class="brush: csharp">
          protected void btnUpdate_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(XMLManager.GetFilePath()));
            XmlNode book, title, author, year, price, category;
            XmlAttribute id, lang;

            // <book> element with Id attribute
            book = doc.CreateElement("book");
            id = doc.CreateAttribute("id");
            id.Value = hdnID.Value;
            book.Attributes.Append(id);

            // <title> element
            title = doc.CreateElement("title");
            title.InnerText = txtTitle.Text.Trim();
            book.AppendChild(title);
            
            // <author> element
            author = doc.CreateElement("author");
            author.InnerText = txtAuthor.Text.Trim();
            book.AppendChild(author);

            // <year> element
            year = doc.CreateElement("year");
            year.InnerText = txtYear.Text.Trim();
            book.AppendChild(year);

            // <price> element 
            price = doc.CreateElement("price");
            price.InnerText = txtPrice.Text.Trim();
            book.AppendChild(price);

            category = doc.CreateElement("category");
            category.InnerText = drpCategory.SelectedItem.Text;
            book.AppendChild(category);

            XmlNode oldNode = doc.SelectSingleNode(string.Format("bookstore/book[@id='{0}']",hdnID.Value));
            oldNode.ParentNode.ReplaceChild(book,oldNode);

            doc.Save(Server.MapPath(XMLManager.GetFilePath()));
            Response.Redirect("ViewXML.aspx");
        }
</pre>

<br/ ><br />
<b>Search XML file using LINQ </b> <br />

In the above code first we create a Book node with required attributes and child nodes. Then we select the node that we want to update by using XPath. After that we replace the existing node with our newly created node and save the XML document. 

<script type="text/javascript">
     SyntaxHighlighter.all()
</script>
<div style='clear: both;'></div>
</div>
<div class='post-footer'>
<div class='post-footer-line post-footer-line-1'>
<span class='post-author vcard'>
Posted by
<span class='fn' itemprop='author' itemscope='itemscope' itemtype='http://schema.org/Person'>
<meta content='https://www.blogger.com/profile/00966916158670098102' itemprop='url'/>
<a class='g-profile' href='https://www.blogger.com/profile/00966916158670098102' rel='author' title='author profile'>
<span itemprop='name'>Kasun Kularatne</span>
</a>
</span>
</span>
<span class='post-timestamp'>
at
<meta content='http://kasunkularatne.blogspot.com/2011/10/xml-manipulation-using-net-c.html' itemprop='url'/>
<a class='timestamp-link' href='https://kasunkularatne.blogspot.com/2011/10/xml-manipulation-using-net-c.html' rel='bookmark' title='permanent link'><abbr class='published' itemprop='datePublished' title='2011-10-30T04:02:00-07:00'>4:02 AM</abbr></a>
</span>
<span class='post-comment-link'>
<a class='comment-link' href='https://kasunkularatne.blogspot.com/2011/10/xml-manipulation-using-net-c.html#comment-form' onclick=''>
No comments:
  </a>
</span>
<span class='post-icons'>
<span class='item-control blog-admin pid-1919009760'>
<a href='https://www.blogger.com/post-edit.g?blogID=7709464623091465853&postID=2946624625076777494&from=pencil' title='Edit Post'>
<img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/>
</a>
</span>
</span>
<div class='post-share-buttons goog-inline-block'>
<a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=7709464623091465853&postID=2946624625076777494&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=7709464623091465853&postID=2946624625076777494&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=7709464623091465853&postID=2946624625076777494&target=twitter' target='_blank' title='Share to Twitter'><span class='share-button-link-text'>Share to Twitter</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=7709464623091465853&postID=2946624625076777494&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=7709464623091465853&postID=2946624625076777494&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a>
</div>
</div>
<div class='post-footer-line post-footer-line-2'>
<span class='post-labels'>
</span>
</div>
<div class='post-footer-line post-footer-line-3'>
<span class='post-location'>
</span>
</div>
</div>
</div>
</div>

        </div></div>
      
</div>
<div class='blog-pager' id='blog-pager'>
<span id='blog-pager-newer-link'>
<a class='blog-pager-newer-link' href='https://kasunkularatne.blogspot.com/' id='Blog1_blog-pager-newer-link' title='Newer Posts'>Newer Posts</a>
</span>
<span id='blog-pager-older-link'>
<a class='blog-pager-older-link' href='https://kasunkularatne.blogspot.com/search?updated-max=2011-10-30T04:02:00-07:00&max-results=1' id='Blog1_blog-pager-older-link' title='Older Posts'>Older Posts</a>
</span>
<a class='home-link' href='https://kasunkularatne.blogspot.com/'>Home</a>
</div>
<div class='clear'></div>
<div class='blog-feeds'>
<div class='feed-links'>
Subscribe to:
<a class='feed-link' href='https://kasunkularatne.blogspot.com/feeds/posts/default' target='_blank' type='application/atom+xml'>Posts (Atom)</a>
</div>
</div>
</div></div>
</div>
</div>
<div class='column-left-outer'>
<div class='column-left-inner'>
<aside>
</aside>
</div>
</div>
<div class='column-right-outer'>
<div class='column-right-inner'>
<aside>
<div class='sidebar section' id='sidebar-right-1'><div class='widget BlogArchive' data-version='1' id='BlogArchive1'>
<h2>Blog Archive</h2>
<div class='widget-content'>
<div id='ArchiveList'>
<div id='BlogArchive1_ArchiveList'>
<ul class='hierarchy'>
<li class='archivedate expanded'>
<a class='toggle' href='javascript:void(0)'>
<span class='zippy toggle-open'>

        ▼ 
      
</span>
</a>
<a class='post-count-link' href='https://kasunkularatne.blogspot.com/2011/'>
2011
</a>
<span class='post-count' dir='ltr'>(5)</span>
<ul class='hierarchy'>
<li class='archivedate collapsed'>
<a class='toggle' href='javascript:void(0)'>
<span class='zippy'>

        ► 
      
</span>
</a>
<a class='post-count-link' href='https://kasunkularatne.blogspot.com/2011/11/'>
November
</a>
<span class='post-count' dir='ltr'>(1)</span>
</li>
</ul>
<ul class='hierarchy'>
<li class='archivedate expanded'>
<a class='toggle' href='javascript:void(0)'>
<span class='zippy toggle-open'>

        ▼ 
      
</span>
</a>
<a class='post-count-link' href='https://kasunkularatne.blogspot.com/2011/10/'>
October
</a>
<span class='post-count' dir='ltr'>(1)</span>
<ul class='posts'>
<li><a href='https://kasunkularatne.blogspot.com/2011/10/xml-manipulation-using-net-c.html'>XML Manipulation using .NET (C#) PART I</a></li>
</ul>
</li>
</ul>
<ul class='hierarchy'>
<li class='archivedate collapsed'>
<a class='toggle' href='javascript:void(0)'>
<span class='zippy'>

        ► 
      
</span>
</a>
<a class='post-count-link' href='https://kasunkularatne.blogspot.com/2011/09/'>
September
</a>
<span class='post-count' dir='ltr'>(1)</span>
</li>
</ul>
<ul class='hierarchy'>
<li class='archivedate collapsed'>
<a class='toggle' href='javascript:void(0)'>
<span class='zippy'>

        ► 
      
</span>
</a>
<a class='post-count-link' href='https://kasunkularatne.blogspot.com/2011/02/'>
February
</a>
<span class='post-count' dir='ltr'>(1)</span>
</li>
</ul>
<ul class='hierarchy'>
<li class='archivedate collapsed'>
<a class='toggle' href='javascript:void(0)'>
<span class='zippy'>

        ► 
      
</span>
</a>
<a class='post-count-link' href='https://kasunkularatne.blogspot.com/2011/01/'>
January
</a>
<span class='post-count' dir='ltr'>(1)</span>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class='clear'></div>
</div>
</div></div>
</aside>
</div>
</div>
</div>
<div style='clear: both'></div>
<!-- columns -->
</div>
<!-- main -->
</div>
</div>
<div class='main-cap-bottom cap-bottom'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
</div>
<footer>
<div class='footer-outer'>
<div class='footer-cap-top cap-top'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
<div class='fauxborder-left footer-fauxborder-left'>
<div class='fauxborder-right footer-fauxborder-right'></div>
<div class='region-inner footer-inner'>
<div class='foot no-items section' id='footer-1'></div>
<table border='0' cellpadding='0' cellspacing='0' class='section-columns columns-2'>
<tbody>
<tr>
<td class='first columns-cell'>
<div class='foot section' id='footer-2-1'><div class='widget Profile' data-version='1' id='Profile1'>
<h2>About Me</h2>
<div class='widget-content'>
<a href='https://www.blogger.com/profile/00966916158670098102'><img alt='My photo' class='profile-img' height='80' src='//2.bp.blogspot.com/_cr0-P_sClrk/S032Xj4wxYI/AAAAAAAAAAQ/3lC4vCoeSD4/S220-s80/DSC00055.JPG' width='60'/></a>
<dl class='profile-datablock'>
<dt class='profile-data'>
<a class='profile-name-link g-profile' href='https://www.blogger.com/profile/00966916158670098102' rel='author' style='background-image: url(//www.blogger.com/img/logo-16.png);'>
Kasun Kularatne
</a>
</dt>
</dl>
<a class='profile-link' href='https://www.blogger.com/profile/00966916158670098102' rel='author'>View my complete profile</a>
<div class='clear'></div>
</div>
</div></div>
</td>
<td class='columns-cell'>
<div class='foot no-items section' id='footer-2-2'></div>
</td>
</tr>
</tbody>
</table>
<!-- outside of the include in order to lock Attribution widget -->
<div class='foot section' id='footer-3' name='Footer'><div class='widget Attribution' data-version='1' id='Attribution1'>
<div class='widget-content' style='text-align: center;'>
Simple theme. Powered by <a href='https://www.blogger.com' target='_blank'>Blogger</a>.
</div>
<div class='clear'></div>
</div></div>
</div>
</div>
<div class='footer-cap-bottom cap-bottom'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
</div>
</footer>
<!-- content -->
</div>
</div>
<div class='content-cap-bottom cap-bottom'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
</div>
</div>
<script type='text/javascript'>
    window.setTimeout(function() {
        document.body.className = document.body.className.replace('loading', '');
      }, 10);
  </script>

<script type="text/javascript" src="https://www.blogger.com/static/v1/widgets/4140855455-widgets.js"></script>
<script type='text/javascript'>
window['__wavt'] = 'AOuZoY6HoD-PyrjJcXDBM2elohQUEoCoKg:1726558884403';_WidgetManager._Init('//www.blogger.com/rearrange?blogID\x3d7709464623091465853','//kasunkularatne.blogspot.com/2011/10/','7709464623091465853');
_WidgetManager._SetDataContext([{'name': 'blog', 'data': {'blogId': '7709464623091465853', 'title': 'Code [A]lpha', 'url': 'https://kasunkularatne.blogspot.com/2011/10/', 'canonicalUrl': 'http://kasunkularatne.blogspot.com/2011/10/', 'homepageUrl': 'https://kasunkularatne.blogspot.com/', 'searchUrl': 'https://kasunkularatne.blogspot.com/search', 'canonicalHomepageUrl': 'http://kasunkularatne.blogspot.com/', 'blogspotFaviconUrl': 'https://kasunkularatne.blogspot.com/favicon.ico', 'bloggerUrl': 'https://www.blogger.com', 'hasCustomDomain': false, 'httpsEnabled': true, 'enabledCommentProfileImages': true, 'gPlusViewType': 'FILTERED_POSTMOD', 'adultContent': false, 'analyticsAccountNumber': '', 'encoding': 'UTF-8', 'locale': 'en', 'localeUnderscoreDelimited': 'en', 'languageDirection': 'ltr', 'isPrivate': false, 'isMobile': false, 'isMobileRequest': false, 'mobileClass': '', 'isPrivateBlog': false, 'isDynamicViewsAvailable': true, 'feedLinks': '\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Code [A]lpha - Atom\x22 href\x3d\x22https://kasunkularatne.blogspot.com/feeds/posts/default\x22 /\x3e\n\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/rss+xml\x22 title\x3d\x22Code [A]lpha - RSS\x22 href\x3d\x22https://kasunkularatne.blogspot.com/feeds/posts/default?alt\x3drss\x22 /\x3e\n\x3clink rel\x3d\x22service.post\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Code [A]lpha - Atom\x22 href\x3d\x22https://www.blogger.com/feeds/7709464623091465853/posts/default\x22 /\x3e\n', 'meTag': '', 'adsenseHostId': 'ca-host-pub-1556223355139109', 'adsenseHasAds': false, 'adsenseAutoAds': false, 'boqCommentIframeForm': true, 'loginRedirectParam': '', 'view': '', 'dynamicViewsCommentsSrc': '//www.blogblog.com/dynamicviews/4224c15c4e7c9321/js/comments.js', 'dynamicViewsScriptSrc': '//www.blogblog.com/dynamicviews/5702e3d62c3de6e9', 'plusOneApiSrc': 'https://apis.google.com/js/platform.js', 'disableGComments': true, 'interstitialAccepted': false, 'sharing': {'platforms': [{'name': 'Get link', 'key': 'link', 'shareMessage': 'Get link', 'target': ''}, {'name': 'Facebook', 'key': 'facebook', 'shareMessage': 'Share to Facebook', 'target': 'facebook'}, {'name': 'BlogThis!', 'key': 'blogThis', 'shareMessage': 'BlogThis!', 'target': 'blog'}, {'name': 'Twitter', 'key': 'twitter', 'shareMessage': 'Share to Twitter', 'target': 'twitter'}, {'name': 'Pinterest', 'key': 'pinterest', 'shareMessage': 'Share to Pinterest', 'target': 'pinterest'}, {'name': 'Email', 'key': 'email', 'shareMessage': 'Email', 'target': 'email'}], 'disableGooglePlus': true, 'googlePlusShareButtonWidth': 0, 'googlePlusBootstrap': '\x3cscript type\x3d\x22text/javascript\x22\x3ewindow.___gcfg \x3d {\x27lang\x27: \x27en\x27};\x3c/script\x3e'}, 'hasCustomJumpLinkMessage': false, 'jumpLinkMessage': 'Read more', 'pageType': 'archive', 'pageName': 'October 2011', 'pageTitle': 'Code [A]lpha: October 2011'}}, {'name': 'features', 'data': {}}, {'name': 'messages', 'data': {'edit': 'Edit', 'linkCopiedToClipboard': 'Link copied to clipboard!', 'ok': 'Ok', 'postLink': 'Post Link'}}, {'name': 'template', 'data': {'name': 'Simple', 'localizedName': 'Simple', 'isResponsive': false, 'isAlternateRendering': false, 'isCustom': false, 'variant': 'pale', 'variantId': 'pale'}}, {'name': 'view', 'data': {'classic': {'name': 'classic', 'url': '?view\x3dclassic'}, 'flipcard': {'name': 'flipcard', 'url': '?view\x3dflipcard'}, 'magazine': {'name': 'magazine', 'url': '?view\x3dmagazine'}, 'mosaic': {'name': 'mosaic', 'url': '?view\x3dmosaic'}, 'sidebar': {'name': 'sidebar', 'url': '?view\x3dsidebar'}, 'snapshot': {'name': 'snapshot', 'url': '?view\x3dsnapshot'}, 'timeslide': {'name': 'timeslide', 'url': '?view\x3dtimeslide'}, 'isMobile': false, 'title': 'Code [A]lpha', 'description': ':: Impossible is not a fact. It\x27s an opinion ::', 'url': 'https://kasunkularatne.blogspot.com/2011/10/', 'type': 'feed', 'isSingleItem': false, 'isMultipleItems': true, 'isError': false, 'isPage': false, 'isPost': false, 'isHomepage': false, 'isArchive': true, 'isLabelSearch': false, 'archive': {'year': 2011, 'month': 10, 'rangeMessage': 'Showing posts from October, 2011'}}}]);
_WidgetManager._RegisterWidget('_NavbarView', new _WidgetInfo('Navbar1', 'navbar', document.getElementById('Navbar1'), {}, 'displayModeFull'));
_WidgetManager._RegisterWidget('_HeaderView', new _WidgetInfo('Header1', 'header', document.getElementById('Header1'), {}, 'displayModeFull'));
_WidgetManager._RegisterWidget('_BlogView', new _WidgetInfo('Blog1', 'main', document.getElementById('Blog1'), {'cmtInteractionsEnabled': false, 'lightboxEnabled': true, 'lightboxModuleUrl': 'https://www.blogger.com/static/v1/jsbin/2675689289-lbx.js', 'lightboxCssUrl': 'https://www.blogger.com/static/v1/v-css/13464135-lightbox_bundle.css'}, 'displayModeFull'));
_WidgetManager._RegisterWidget('_BlogArchiveView', new _WidgetInfo('BlogArchive1', 'sidebar-right-1', document.getElementById('BlogArchive1'), {'languageDirection': 'ltr', 'loadingMessage': 'Loading\x26hellip;'}, 'displayModeFull'));
_WidgetManager._RegisterWidget('_ProfileView', new _WidgetInfo('Profile1', 'footer-2-1', document.getElementById('Profile1'), {}, 'displayModeFull'));
_WidgetManager._RegisterWidget('_AttributionView', new _WidgetInfo('Attribution1', 'footer-3', document.getElementById('Attribution1'), {}, 'displayModeFull'));
</script>
</body>
</html>