In order to start with XML Parsing or XML data Handling in Microsoft .NET Framework first we need to know about what classes or namespaces are available to us for using or dealing with XML Document or XML Data in .Net So Here is list of XML Namespaces available for XML Processing and Handling :-
- System.Xml;
- System.Schema;
- System.Xml.Serialization;
- System.Xml.XPath;
- System.Xml.Xsl;
Here , Out of these classes System.Xml class is major class that is responsible or must for XML Data Processing and this is the namespace that we are going to use in our tutorial today . The System.Xml namespace contains number of classes that are used for reading and writting Xml data from XML Documents . System.Xml contains :-
- XmlReader for Reading Xml Documents we are not going to use it in our tutorial today
- XmlWritter For Writting Xml Documents we are not going to use it in our tutorial today
There is another important class that we will use in our XML Parsing today . This is
XmlNode It plays important role for getting xml data node by node This class represents every single node of the XML Document
OUTPUT SCREEN WITH RESULT
Main.Xaml CODE MONO ANDROID
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="@+id/main_table"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="0.0dp"
android:scrollbars="vertical" />
</LinearLayout>
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using System.Xml;
XAML CODE EXPLANATION MONO ANDROID
step 1
In first line i have declared the xml version and encoding type this denotes that this is an xml file code . In android xml code is used for creating design or layout of mono Android Apps
step 2
Now We have added a Linear Layout this is a default layout for Mono Android Application all controls or other layouts that we will use in our Mono Android Application will reside under this LinearLayout Tag
It contains Starting and Ending Tag .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
</LinearLayout>
step 3
Now Under LinearLayout we have included a Table layout We have included Table layout because we in this tutorial of xml parsing is going to parse xml and then view the retrieved xml data in table view . We have used following code for styling or placing table layout
<TableLayout
android:id="@+id/main_table"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="0.0dp"
android:scrollbars="vertical" />
Csharp Code For XML Parsing MONO ANDROID
namespace AndroidApplication5
{
[Activity(Label = "AndroidApplication5", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Product> <ID>1</ID> <SeqNo>1</SeqNo> <Stage>Stage</Stage> <Observation>Observation</Observation><CHK></CHK> </Product>");
XmlNode node = doc.DocumentElement.SelectSingleNode("/Product/ID");
XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Product/SeqNo");
XmlNode node2 = doc.DocumentElement.SelectSingleNode("/Product/Stage");
XmlNode node3 = doc.DocumentElement.SelectSingleNode("/Product/Observation");
XmlNode node4 = doc.DocumentElement.SelectSingleNode("/Product/CHK");
TableLayout ll = FindViewById<TableLayout>(Resource.Id.main_table);
for (int i = 0; i < 10; i++)
{
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.FillParent);
row.LayoutParameters = lp;
row.SetGravity(GravityFlags.CenterHorizontal);
TextView checkBox = new TextView(this);
TextView tv = new TextView(this);
TextView addBtn = new TextView(this);
TextView rcf = new TextView(this);
CheckBox verified = new CheckBox(this);
TextView ver = new TextView(this);
TextView qty = new TextView(this);
if (i == 1)
{
checkBox.Text = node.Name.ToUpper().ToString();
checkBox.SetBackgroundColor(Color.AliceBlue);
checkBox.SetTextColor(Color.Black);
checkBox.SetPadding(7,9,7,9);
qty.Text = node1.Name.ToUpper().ToString();
qty.SetBackgroundColor(Color.AliceBlue);
qty.SetTextColor(Color.Black);
qty.SetPadding(7, 9, 9, 9);
tv.Text = node2.Name.ToUpper() .ToString();
tv.SetBackgroundColor(Color.AliceBlue);
tv.SetTextColor(Color.Black);
tv.SetPadding(7, 9, 9, 9);
addBtn.Text = node3.Name.ToUpper().ToString();
addBtn.SetBackgroundColor(Color.AliceBlue);
addBtn.SetTextColor(Color.Black);
addBtn.SetPadding(9, 9, 9, 9);
ver.Text = node4.Name.ToUpper().ToString();
ver.SetBackgroundColor(Color.AliceBlue);
ver.SetTextColor(Color.Black);
ver.SetPadding(9, 9, 9, 9);
}
else if (i == 0)
{
checkBox.Text = "Inspection";
checkBox.SetBackgroundColor(Color.AliceBlue);
checkBox.SetTextColor(Color.Black);
checkBox.SetPadding(9, 9, 9, 9);
qty.Text = "Details";
qty.SetBackgroundColor(Color.AliceBlue);
qty.SetTextColor(Color.Black);
qty.SetPadding(9, 9, 9, 9);
tv.Text = "Rail";
tv.SetBackgroundColor(Color.AliceBlue);
tv.SetTextColor(Color.Black);
tv.SetPadding(9, 9, 7, 9);
addBtn.Text = "Coach";
addBtn.SetBackgroundColor(Color.AliceBlue);
addBtn.SetTextColor(Color.Black);
addBtn.SetPadding(7, 9, 9, 9);
ver.Text = "";
ver.SetBackgroundColor(Color.AliceBlue);
ver.SetTextColor(Color.Black);
ver.SetPadding(9, 9, 9, 9);
}
else
{
checkBox.Text = (i-1).ToString();
checkBox.SetBackgroundColor(Color.Black);
checkBox.SetTextColor(Color.White);
checkBox.SetPadding(7,9,9,9);
qty.Text = (i - 1).ToString();
qty.SetBackgroundColor(Color.Black);
qty.SetTextColor(Color.White);
qty.SetPadding(7,9,9,9);
tv.Text = node2.InnerText+(i-1).ToString ();
tv.SetBackgroundColor(Color.Black);
tv.SetTextColor(Color.White);
tv.SetPadding(9, 9, 9, 9);
addBtn.Text = node3.InnerText + (i-1).ToString();
addBtn.SetBackgroundColor(Color.Black);
addBtn.SetTextColor(Color.White);
addBtn.SetPadding(9, 9, 9, 9);
verified.Checked = true;
}
checkBox.Gravity = GravityFlags.CenterHorizontal;
tv.Gravity = GravityFlags.CenterHorizontal;
qty.Gravity = GravityFlags.CenterHorizontal;
addBtn.Gravity = GravityFlags.CenterHorizontal;
row.AddView(checkBox);
row.AddView(qty);
row.AddView(tv);
row.AddView(addBtn);
if (i == 1)
{
row.AddView(ver);
}
else if(i>1)
{
row.AddView(verified);
}
ll.AddView(row, i);
}
}
}
}
CsharpCode Explanation For XML Parsing MONO ANDROID
step1
we start with main login of xml parsing that we are covering in our this tutorial here we have first include System.Xml namespace at top of our code document then we have used a XmlDocument class by making an instance of that class then we have passed the static Xml data to instance of XmlDocument by using LoadXml class that receives the xml data . you can see code below :-
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Product> <ID>1</ID> <SeqNo>1</SeqNo> <Stage>Stage</Stage> <Observation>Observation</Observation><CHK></CHK> </Product>");
Step 2
After passing the xml data now we are going to filter the xml data with respect to nodes of xml In our xml data Product is Parent node and it contains other child node like ID,SeqNo ..........
As we have loaded the xml data in doc we have used doc.Documentelement.SelectSingleNode that will select the single node of 'ID' that is child of Parent node 'Product'
You can see the code below :-
XmlNode node = doc.DocumentElement.SelectSingleNode("/Product/ID");
XmlNode node1 = doc.DocumentElement.SelectSingleNode("/Product/SeqNo");
XmlNode node2 = doc.DocumentElement.SelectSingleNode("/Product/Stage");
XmlNode node3 = doc.DocumentElement.SelectSingleNode("/Product/Observation");
XmlNode node4 = doc.DocumentElement.SelectSingleNode("/Product/CHK");
step 3
With these lines of code below we are select the table layout that we have added in our xml code in layout file . You can see the code below :-
TableLayout ll = FindViewById<TableLayout>(Resource.Id.main_table);
Step 4
This is main logic of code where we are reading xml data and then adding dynamic rows to table layout
for (int i = 0; i < 10; i++) // starting loop form 0 -9 to display 10 rows in table dynamically
{
//creating a new tablerow
TableRow row = new TableRow(this);
//Setting tablerow hight , width parameters
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.FillParent);
assigning the layoutparameters to row
row.LayoutParameters = lp;
//setting row to alignment to center horizontal
row.SetGravity(GravityFlags.CenterHorizontal);
//Adding some required TextView
TextView checkBox = new TextView(this);
TextView tv = new TextView(this);
TextView addBtn = new TextView(this);
TextView rcf = new TextView(this);
CheckBox verified = new CheckBox(this);
TextView ver = new TextView(this);
TextView qty = new TextView(this);
if (i == 1) // this is for second row of table to display column names not values
{
checkBox.Text = node.Name.ToUpper().ToString();
checkBox.SetBackgroundColor(Color.AliceBlue);
checkBox.SetTextColor(Color.Black);
checkBox.SetPadding(7,9,7,9);
qty.Text = node1.Name.ToUpper().ToString();
qty.SetBackgroundColor(Color.AliceBlue);
qty.SetTextColor(Color.Black);
qty.SetPadding(7, 9, 9, 9);
tv.Text = node2.Name.ToUpper() .ToString();
tv.SetBackgroundColor(Color.AliceBlue);
tv.SetTextColor(Color.Black);
tv.SetPadding(7, 9, 9, 9);
addBtn.Text = node3.Name.ToUpper().ToString();
addBtn.SetBackgroundColor(Color.AliceBlue);
addBtn.SetTextColor(Color.Black);
addBtn.SetPadding(9, 9, 9, 9);
ver.Text = node4.Name.ToUpper().ToString();
ver.SetBackgroundColor(Color.AliceBlue);
ver.SetTextColor(Color.Black);
ver.SetPadding(9, 9, 9, 9);
}
else if (i == 0) //this is for first row to display Heading
{
checkBox.Text = "Inspection";
checkBox.SetBackgroundColor(Color.AliceBlue);
checkBox.SetTextColor(Color.Black);
checkBox.SetPadding(9, 9, 9, 9);
qty.Text = "Details";
qty.SetBackgroundColor(Color.AliceBlue);
qty.SetTextColor(Color.Black);
qty.SetPadding(9, 9, 9, 9);
tv.Text = "Rail";
tv.SetBackgroundColor(Color.AliceBlue);
tv.SetTextColor(Color.Black);
tv.SetPadding(9, 9, 7, 9);
addBtn.Text = "Coach";
addBtn.SetBackgroundColor(Color.AliceBlue);
addBtn.SetTextColor(Color.Black);
addBtn.SetPadding(7, 9, 9, 9);
ver.Text = "";
ver.SetBackgroundColor(Color.AliceBlue);
ver.SetTextColor(Color.Black);
ver.SetPadding(9, 9, 9, 9);
}
//else is used for all other rows to be added dynamically except 0,1 row
else
{
checkBox.Text = (i-1).ToString();
checkBox.SetBackgroundColor(Color.Black);
checkBox.SetTextColor(Color.White);
checkBox.SetPadding(7,9,9,9);
qty.Text = (i - 1).ToString();
qty.SetBackgroundColor(Color.Black);
qty.SetTextColor(Color.White);
qty.SetPadding(7,9,9,9);
tv.Text = node2.InnerText+(i-1).ToString ();
tv.SetBackgroundColor(Color.Black);
tv.SetTextColor(Color.White);
tv.SetPadding(9, 9, 9, 9);
addBtn.Text = node3.InnerText + (i-1).ToString();
addBtn.SetBackgroundColor(Color.Black);
addBtn.SetTextColor(Color.White);
addBtn.SetPadding(9, 9, 9, 9);
verified.Checked = true;
}
checkBox.Gravity = GravityFlags.CenterHorizontal;
tv.Gravity = GravityFlags.CenterHorizontal;
qty.Gravity = GravityFlags.CenterHorizontal;
addBtn.Gravity = GravityFlags.CenterHorizontal;
row.AddView(checkBox);
row.AddView(qty);
row.AddView(tv);
row.AddView(addBtn);
if (i == 1)
{
row.AddView(ver);
}
else if(i>1)
{
row.AddView(verified);
}
ll.AddView(row, i);
}