Naveen's Weblog

Bridge to future

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 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: