Umbraco: XML Serialization of custom data value in umbraco.

In this post we will consider to solve this issue:

  •   How to store xml parseble data in umbraco.
  •   How to store custom data type value in umbraco database
  •   How to store serialize data in umbraco database for custom data type

In umbraco when we create custom data type we want to store custom data property value in data base in xml serialized format.For instance we have a custom data type called first name and last name field in it and i want to save serialized data in data base correspond to property.

Below format of data provide easy  way of data manipulation at fronted side. If you want to see xml format of data which you have entered and published open umbraco config file you will find there data in below format. which can be easily manipulate on front end.

<Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Nishant</FirstName>
<LastName>Sharma</LastName>
<Age>57</Age>
<statusvalue3>s</statusvalue3>
</Name>

At First we will create custom data type Let say custom user control which will contain first name , last name , age.

CustomDataType.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomDataTypes.ascx.cs" Inherits="CustomDataType.usercontrols.CustomDataTypes" %>
<table>
<tr><td><asp:TextBox ID="FirstName" runat="server"></asp:TextBox></td></tr>
<tr><td><asp:TextBox ID="LastName" runat="server"></asp:TextBox></td></tr>
<tr><td><asp:TextBox ID="Age" runat="server"></asp:TextBox></td></tr>
</table>

Code Behind file for CustomDataType.ascx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.editorControls;
using umbraco.cms.businesslogic.web;
using umbraco.editorControls.userControlGrapper;
using umbraco.cms.businesslogic.datatype;
using CustomDataType;
using System.IO;
using System.Text;

namespace CustomDataType.usercontrols
{
    public partial class CustomDataTypes :
     // Inherit umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
     System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {

	// Define here private type of valye
	// Here Name is Class
        private Name _umbval;
        public object value
        {
            get
            {
                var name = GetName();
		//  Serialize the value here in xml format.
                return SerializationHelper.ValueToXmlString(name);
            }
            set
            {
	        // Handle null value here.
                if (value == null || string.IsNullOrEmpty(value.ToString()))
                {
                    _umbval = Name.Empty;
                }
                else
                {
		    // Geting xml to string format value and store it in _umbval
                    _umbval = (Name)SerializationHelper.ValueFromXmlString(value, typeof(Name));
                }
            }
        }

	// This method return Name class object called name which is used as var name = GetName();
	// basically we are doing serialization of data here our goal is to keep data in db as xml.
        public Name GetStatus()
        {
            Name name = new Name();
            name.FirstName =  FirstName.Text;
            name.LastName = LastName.Text;
            name.Age = Age.Text;
            return name;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
	    // When you publish any document in umbraco it do
	    // postback so at post back we are assigning the entered value in textbox
	    // you should know here i am geting value from _umbval which is in string
	    // format : that's why we have written _umbval = (Name)SerializationHelper.ValueFromXmlString(value, typeof(Name));
	    // in set above code.
            if (!IsPostBack)
	    {
                FirstName.Text = _umbval.StatusValue;
                LastName.Text = _umbval.StatusValue1;
                Age.Text = _umbval.StatusValue2;
            }
        }

    }
}

At the end we have to define Name class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace CustomDataType
{
    [Serializable]
    [XmlRoot("Name")]
    public class Name
    {

            [XmlElement("FirstName")]
            public string FirstName { get; set; }

            [XmlElement("LastName")]
            public string LastName { get; set; }

            [XmlElement("Age")]
            public string Age { get; set; }

            public static Name Empty
            {
                get
                {
                    var emptyName = new Name();
                    emptyName = null;
                    return emptyName;
                }
            }
    }
}

At the end Build your solution and try it , It will work fine , In any problem feel free to comment.

Page Keyword: serialization in umbraco,xml serialization in umbraco, DatatypesXML Serialization, Custom DatatypesXML Serialization,DatatypesXML SerializationStore rich data

Another Umbraco Article

Advertisements

3 thoughts on “Umbraco: XML Serialization of custom data value in umbraco.

  1. Pingback: Umbraco: How to Create Custom data type in umbraco using c#. | [Code Destination]

  2. Pingback: Create your first Rss Feed Burner in umbraco | [Code Destination]

  3. Pingback: Install umbraco manually. | [Code Destination]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s