Retrieving parent and child nodes from a Firebase database through use of a C# application.
Here we will be going through the process of retrieving parent and child nodes from the Firebase database using a C# application. The aim of this project was to allow for the ability to retrieve the child nodes of any given parent. We will now move on to the different fragments of code starting off with the system header files that are being used. The project is made up of a stream reader that retrieves and reads the data within the database. We then convert and parse the stream as a JObject and find the child nodes within a single parent.
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
In the code below we start off by creating a web request to our database project in Firebase using its URL. We then stream the response making use of a stream reader. In this example the stream reader is continuously running which wouldn’t be effective in real life. However one could change this to be invoked at certain times when needed and then closing the connection when idle.
string Url = ".firebaseio.com/.json";
var httprequest = (HttpWebRequest)WebRequest.Create(Url);
var response = (HttpWebResponse)httprequest.GetResponse();
var stream = new StreamReader(response.GetResponseStream()).ReadToEnd();
Whilst this project is running we are also running another application in the background that is constantly updating children nodes within our Firebase database. The application is making use of the current time where the hour, minute and second have been declared as individual variables and are being placed in to the database. Below can be seen how the structure of our current database is perceived.
Time {
Hour : 04
Minute : 35
Second: 24
}
Looking at the above we can see the parent node to be named “Time”, consisting of 3 child nodes named “Hour”, “Minute” and “Second”.
In the code fragment below we create a new variable which is the data within the stream reader being parsed as a JObject. We then see the creation of 3 new strings named “child1”, “child2” and “child3” which are the children nodes within the parent named “Time”.
var variable = JObject.Parse(stream);
string child1 = (string)variable["Time"]["Hour"];
string child2 = (string)variable["Time"]["Minute"];
string child3 = (string)variable["Time"]["Second"];
Console.WriteLine(child1 +":"+ child2 + ":" + child3);
In the above we are looking for the parent node named “Time” within Firebase. We then see “Hour”, “Minute” and “Seconds” for each of the strings which are our children nodes. When the parent and child are found the data is then stored in to this string and presented to us within the console.
Source code in full is found below, be sure that you have installed the Newtonsoft.Json package.
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
while (true)
{
string Url = ".firebaseio.com/.json";
var httprequest = (HttpWebRequest)WebRequest.Create(Url);
var response = (HttpWebResponse)httprequest.GetResponse();
var stream = new StreamReader(response.GetResponseStream()).ReadToEnd();
var variable = JObject.Parse(stream);
string child1 = (string)variable["Time"]["Hour"];
string child2 = (string)variable["Time"]["Minute"];
string child3 = (string)variable["Time"]["Second"];
Console.WriteLine(child1 +":"+ child2 + ":" + child3);
}
}