C# – LINQ to read XML

LINQ to read XML

Loading the XML File

XElement and XDocument derive from XContainer.

1. XElement class

Reads the XML elements (attributes and children)

Implements IXmlSerializable.

XElement xelement = XElement.Load(file);
IEnumerable employees = xelement.Elements();
//Read the entire XML
foreach(var employee in employees)
{
	Console.WriteLine("Reading Employee: == " + employee);
}

2. XDocument class

Reads the entire XMl document.

Reads from the single root element.

XDocument xdocument = XDocument.Load(file);
IEnumerable employees = xdocument.Elements();
foreach(var employee in employees)
{
     Console.WriteLine("Reading Employee: == " + employee);
}

Reading the Single Element

foreach(var employee in employees)
{
     Console.WriteLine("Reading Employee: == " + employee.Element("Name").Value);
}

Reading Multiple Elements

foreach(var employee in employees)
{
Console.WriteLine("Reading Employee: {0} == {1}", employee.Element("Name").Value, employee.Element("EmpId").Value);
}

Filter Elements

XElement xelement = XElement.Load(file);
var name = from nm in xelement.Elements("Employee")
    where (string)nm.Element("Sex") == "Female"
    select nm;

    Console.WriteLine("Information about Female Employees");
    foreach(XElement x in name)
    {
	Console.WriteLine(x);
    }

Filter Element within another Element

XElement xelement = XElement.Load(file);
var addresses = from address in xelement.Elements("Employee")
	where (string)address.Element("Address").Element("City") == "Alta"
	select address;

Console.WriteLine("Information about Employees in Alta City");
foreach(XElement x in addresses)
{
	Console.WriteLine(x);
}

Find Nested Elements using Descendants

XElement xelement = XElement.Load(file);
Console.WriteLine("List of all Zip codes");
foreach(XElement x in xelement.Descendants("Zip"))
{
    Console.WriteLine((string)x);
}

Sorting Elements

XElement xelement = XElement.Load(file);
IEnumerable codes = from code in xelement.Elements("Employee")
	let zip = (string)code.Element("Address").Element("Zip")
        orderby zip
	select zip;
Console.WriteLine("List and Sort all Zip Codes");
			 
foreach (string zp in codes)
	Console.WriteLine(zp);

Gayatri Mantra

Gayatri Mantra

A Universal Prayer enshrined in the Vedas:

Om Bhur Bhuvaḥ Swaḥ
Tat-savitur Vareñyaṃ
Bhargo Devasya Dhīmahi
Dhiyo Yonaḥ Prachodayāt

According to the Vedas:

Chanting the Gayatri Mantra purifies the chanter

Listening to the Gayatri Mantra purifies the listener

Meaning:

gaya: – vital energies

tri: – preserves, protects, gives deliverance, grants liberation

Word meaning:

Om The primeval sound;
Bhur The physical body/physical realm;
Bhuvah The life force/the mental realm
Suvah The soul/spiritual realm;
Tat That (God);
Savitur The Sun, Creator (source of all life);
Vareñyam Adore;
Bhargo Effulgence (divine light);
Devasya Supreme Lord;
Dhīmahi Meditate;
Dhiyo:- The intellect;
Yo May this light;
Nah Our;
Prachodayāt Illumine/inspire.

The mantra is a hymm to Savitur, the sun god.

One can seek wisdom and enlightenment by chanting the gayatri mantra.

Goddess Gayatri is also called “Veda-Mata” or the Mother of the Vedas – Rig, Yajur, Saam and Atharva.

We meditate on that most adorable, desirable and enchanting luster and brilliance of our Supreme Being, our Source Energy, our Collective Consciousness….who is our creator, inspirer and source of eternal Joy. May this warm and loving Light inspire and guide our mind and open our hearts.

Chanting

Ideal times to chant the mantra are 3 times a day – at dawn, mid-day and at dusk. The three sandhyas – morning, mid-day and evening.

Maximum benefit of chanting the mantras can be attained by 108 times. But it can also be chanted 3,9 or 18 times.

Time, has three qualities:
(4am – 8am/ 4pm 8pm) satva – qualities of purity or serenity
(8am – 4pm) rajas – passion
(8pm – 4am) tamas – inaction

Benefits

Gayatri Mantra removes all obstacles in our path to increased wisdom and spiritual growth and development.

Righteous wisdom starts emerging soon after Jap(recitation) of the Gayatri Mantra is performed.

The syllables of the mantra are said to positively affect all the chakras or energy centres in the human body.

Measuring Execution Times

Measuring Execution Times

The SQL Server Management Studio shows query measurement time in seconds. If we are concern about performance then in-depth measurement is required such as milliseconds.

Using Statistics

set statistics time on

-- your query

set statistics time off

Messages Tab
SQL Server parse and compile time:
CPU time = 67 ms, elapsed time = 67ms

Setting the timing as default for every query
Query -> query options -> advanced -> Execution
check the “set statistics time” checkbox
Check “set statistics IO” checkbox

Using Client Statistics

Ways to turn on the Client Statistics

  • Menu: Query -> Include client Statistics
  • Toolbar: Click button Include Statistics
  • Keyboard: Shift+Alt+S

Properties

Client Execution Time Time the trial was started.
Query Profile Statistics
Number of INSERT, DELETE and UPDATE statements The number of Insert, Delete or Update statements that were executed in that particular trail.
Rows affected by INSERT, DELETE or UPDATE statements Number of rows that were affected by Insert, Delete or update statement part of your trial.
Number of SELECT statements Number of select statement that were executed under that particular trial execution. It includes fetch statements to retrieve rows from cursors.
Rows returned by SELECT statements Rows selected as part of that trail execution.
Number of transactions User transactions used in a trail execution.
Network Statistics How much traffic is moving from client to the server and back.
Number of server roundtrips Number of times request sent to server and number of time reply received from server in a trail execution.
TDS packets sent from client Number of TDS packets client has sent to the database server under a trial execution.
TDS packets received from server The number of TDS packets received by client from database server under a trial execution.
Bytes sent from client The number of bytes that the client has sent to database server under a trial execution. Includes spaces selected after query as well.
Time Statistics How much time was spent processing on the client versus how much time was spent waiting for the server in milliseconds.
Client processing time The cumulative amount of time that the client spent in executing code while the query was executed
Total execution time The cumulative amount of time (in milliseconds) that the client spent processing while the query was executed, including the time that the client spent waiting for replies from the server as well as the time spent executing code.
Wait time on server replies The cumulative amount of time (in milliseconds) that the client spent while it waited for the server to reply.

By default Client Statistics shows up to 10 trails. After 10 trails, every trail position get decreased by 1 and trail 0 get removed from list.

Reset Client Statistics
Query Menu -> Reset Client Statistics

Using Execution Time as Variable

DECLARE @StartTime datetime
DECLARE @EndTime datetime
SELECT @StartTime=GETDATE()

--Query goes here

SELECT @EndTime=GETDATE()
SELECT DATEDIFF(ms, @StartTime, @EndTime) AS [Duration in millisecs]