Naveen's Weblog

Bridge to future

Archive for the ‘LINQ to XML’ Category

Usage of LINQ to XML

Processes to XML using LINQ

Posted by codingsense on September 26, 2008


We have seen what is LINQ to objects and LINQ to XML now lets do some work that would need the combination of both.

Let us create an XML file which will consist of top 4 processes that are consuming high memory.

var MyProcess = from Proc in Process.GetProcesses()
where Proc.PagedMemorySize64 > 10000000
orderby Proc.PagedMemorySize64 descending
select new
XElement(“Process”,
new XElement(“Name”, Proc.ProcessName),
new XElement(“Memory”, Proc.PagedMemorySize64)
);

Console.WriteLine(new XElement(“Processes”, MyProcess.Take(4)));


Just have a closer look at what is being done. In MyProcess variable we get the list of all the process in the system those are taking more than 10000000 bytes of memory in desc order. While printing in Console.WriteLine we filter the top 4 process using Take Method.

Keep Learning :)

Posted in LINQ to XML | Tagged: , , | Leave a Comment »

Grouping XML Elements using Group By

Posted by codingsense on September 24, 2008

Since you have seen how to create an XML and fetch simple data from it, lets move to next level. In this sample we shall Group the elements and create our own XML from an existing XML.

Download Sample – 26Kb

Lets us consider a clientInfo XML which will store client Name and his Address(City,Area), now consider a situaltion where we would require to list all the employes grouped with their area name, How it can be done?. Here it goes.

XElement GroupAreas = new XElement("Areas",
            from client in ClientInfo.Elements()
            group client by client.Element("Address").Element("Area").Value into GroupedNames
            select new XElement(GroupedNames.Key,
                   GroupedNames.Elements("Name")
            ));

After this the clients will be grouped on their area name.

OutPut:

Happy Learning :)

Posted in LINQ to XML | Tagged: , , | 4 Comments »

Join XML using LINQ

Posted by codingsense on September 23, 2008

Hi All,

Today i have come with joining of 2 XML files using a relation using linq.

I will take a scenario where the clients are recognized by clientID , the basic information of each client (ID, Name and address) is stored at one place and the salary details (ID, Salary) will be stored at another place. Lets see what can be done If we need to join both these files into one.

 

Problem View::

Download Sample – 29kb

Create ClientInfo XML::

Here we shall create an XML of client data. Consider ID to be Unique Number (primary key). For each client we will have details like ID, name and address.

XElement ClientInfo = new XElement("Clients",
new XElement("Client",
new XElement("ID", "1"),
new XElement("Name", "Client1"),
new XElement("Address", "Mahalaxmi")
),
new XElement("Client",
new XElement("ID", "2"),
new XElement("Name", "Client2"),
new XElement("Address", "J.C.Road")
),
new XElement("Client",
new XElement("ID", "3"),
new XElement("Name", "Client3"),
new XElement("Address", "M.G.Road")
)
);

 

Output::

Create Salary XML::

Now we shall create an XML that will store the salary of each clients with their respective client ID.

XElement Salary = new XElement("Salaries",
new XElement("Salary",
new XElement("ID", "1"),
new XElement("Amount", "20000")
),
new XElement("Salary",
new XElement("ID", "3"),
new XElement("Amount", "30000")
),
new XElement("Salary",
new XElement("ID", "3"),
new XElement("Amount", "40000")
)
);

Output::


Join Both XML::

Lets try to join both the XML using the client ID and make them into one XML that will consist some elements of client info and some of salary .

XElement Join = new XElement("Clients",
from client in ClientInfo.Elements()
from sal in Salary.Elements()
where client.Element("ID").Value == sal.Element("ID").Value
select new XElement("Client", client.Descendants(), sal.Elements("Amount"))
);

Output:

Hope all of you got how to join two XML using an element as relation using LINQ.

Keep Exploring :)

Posted in LINQ to XML | Tagged: , , | Leave a Comment »

LINQ to XML basic sample

Posted by codingsense on September 22, 2008

The Microsoft definition:

LINQ to XML provides an in-memory XML programming interface that leverages the .NET Language-Integrated Query (LINQ) Framework. LINQ to XML uses the latest .NET Framework language capabilities and is comparable to an updated, redesigned Document Object Model (DOM) XML programming interface.

The namespace for dealing with XML is System.Xml.Linq

Download Sample – 26.4Kb

Basic Sample of LINQ to XML:

In this sample we will concentrate on how a XML can be created and accessed.
We will create a XML which will hold the client Information’s (ID,Name,Address).

Create a XML element:

XElement ClientInfo = new XElement("Clients",
new XElement("Client",
new XElement("ID", "1"),
new XElement("Name", "Client1"),
new XElement("Address", "Mahalaxmi")
),
new XElement("Client",
new XElement("ID", "2"),
new XElement("Name", "Client2"),
new XElement("Address", "J.C.Road")
),
new XElement("Client",
new XElement("ID", "3"),
new XElement("Name", "Client3"),
new XElement("Address", "M.G.Road")
)
);

Here XElement is an element of XML. It can be a single element or a child of another element or a parent with many more elements within it.

Output:

Only Client ID’s in a string Array::

Now lets try to take only client ID’s in a string array. Here is the code

string[] OnlyIDArr = (from Client in ClientInfo.Elements()
                      select Client.Element("ID").Value).ToArray<string>();

Output:

Create XML with only Client ID’s::

Here we will try to create an XML of only client ID’s

var OnlyID = from Client in ClientInfo.Elements()
             select new XElement("Client", Client.Element("ID"));

Output:

Nowadays XML is playing a vital role in many softwares and LINQ can be utilised to make it much easy, so try to explore it.

Keep Learning :)

Posted in LINQ to XML | Tagged: , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.