×
Namespaces

Variants
Actions

How to convert XML to Class object in Windows phone 8

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to convert Xml to Class Object in Windows Phone 8

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
CompatibilityArticle
Created: munjalrohit (18 Sep 2013)
Last edited: hamishwillee (07 Oct 2013)

Introduction

Sometime, we need to retrieve the data from web service in the form of XML. Ofcourse we can do parsing XML and use in application. But below article explains the alternative method of using XML retrieved from web service without parsing. We can do this by using XML deserialization to class object.

Below are the sample code : Sample XML

<?xml version="1.0" encoding="utf-8" ?>
<Contacts Mobile="98738****9">
<Contact Label="TestNormalAlerts" Type="0">
<Period StartDate="2013-08-28T00:00:00" EndDate="2013-08-29T00:00:00" StartTime="1900-01-01T07:59:21" EndTime="1900-01-01T08:59:28" />
<Friend MSISDN="98738****1" FirstName="Rohit" LastName="Munjal" NickName="MunjalRohit" />
</Contact>
<Contact Label="TestNormalAlerts" Type="0">
<Period StartDate="2013-08-28T00:00:00" EndDate="2013-08-29T00:00:00" StartTime="1900-01-01T07:59:21" EndTime="1900-01-01T08:59:28" />
<Friend Mobile="98738****2" FirstName="Niti" LastName="Munjal" NickName="Niti" />
</Contact>
<Contact Label="TestNormalAlerts" Type="0">
<Period StartDate="2013-08-28T00:00:00" EndDate="2013-08-29T00:00:00" StartTime="1900-01-01T07:59:21" EndTime="1900-01-01T08:59:28" />
<Friend Mobile="98738****3" FirstName="Diya" LastName="Munjal" NickName="diya" />
</Contact>
</Contacts>

Class created for XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
using System.Xml.Serialization;
 
namespace CustomPushPinWithToolTip
{
class ContactClass
{
 
}
 
[XmlRoot("Contacts")]
public class ContactList
{
private string attribute_Mobile;
 
[XmlAttribute("Mobile")]
public string MOBILE
{
get { return attribute_Mobile; }
set { attribute_Mobile = value; }
}
 
[XmlElement("Contact")]
public ContactItem[] ContactItems { get; set; }
 
}
 
[XmlRoot("Contact")]
public class ContactItem
{
private string attribute_Label;
private int attribute_Type;
 
[XmlAttribute("Label")]
public string Label
{
get { return attribute_Label; }
set { attribute_Label = value; }
}
 
[XmlAttribute("Type")]
public int Type
{
get { return attribute_Type; }
set { attribute_Type = value; }
}
 
[XmlElement("Period")]
public Period Period { get; set; }
 
[XmlElement("Friend")]
public FriendItem Contact { get; set; }
 
 
}
 
public class Period
{
private DateTime attribute_StartDate;
private DateTime attribute_EndDate;
private DateTime attribute_StartTime;
private DateTime attribute_EndTime;
[XmlAttribute("StartDate", Form = XmlSchemaForm.Unqualified)]
public DateTime StartDate
{
get { return attribute_StartDate; }
set { attribute_StartDate = value; }
}
 
[XmlAttribute("EndDate", Form = XmlSchemaForm.Unqualified)]
public DateTime EndDate
{
get { return attribute_EndDate; }
set { attribute_EndDate = value; }
}
 
[XmlAttribute("StartTime", Form = XmlSchemaForm.Unqualified)]
public DateTime StartTime
{
get { return attribute_StartTime; }
set { attribute_StartTime = value; }
}
 
[XmlAttribute("EndTime", Form = XmlSchemaForm.Unqualified)]
public DateTime EndTime
{
get { return attribute_EndTime; }
set { attribute_EndTime = value; }
}
}
 
public class FriendItem
{
private string attribute_Mobile;
private string attribute_FirstName;
private string attribute_LastName;
private string attribute_NickName;
 
[XmlAttribute("Mobile")]
public string FriendMobile
{
get { return attribute_Mobile; }
set { attribute_Mobile = value; }
}
 
[XmlAttribute("FirstName")]
public string FirstName
{
get { return attribute_FirstName; }
set { attribute_FirstName = value; }
}
 
[XmlAttribute("LastName")]
public string LastName
{
get { return attribute_LastName; }
set { attribute_LastName = value; }
}
 
[XmlAttribute("NickName")]
public string NickName
{
get { return attribute_NickName; }
set { attribute_NickName = value; }
}
}
 
 
 
 
}

Code for deserialization of XML

private void XmlToObject()
{
 
XmlSerializer serializer = new XmlSerializer(typeof(ContactList));
XDocument document = XDocument.Load("SampleXML.xml");//XDocument.Parse(e.Result);
ContactList ListDataContact = (ContactList)serializer.Deserialize(document.CreateReader());
 
 
}


Summary

This Code example Shows how to use Xml retrieved from web service without parsing the xml. Hope you find this article useful in your Windows Phone 8 application.

This page was last modified on 7 October 2013, at 06:06.
120 page views in the last 30 days.
×