How to get started with Linq to XML
Linq to Xml can save you lots of time when you're dealing with Xml. In this article I'll write a simple application, using Linq to Xml. The results of the application are rather trivial, but the used techniques are interesting.
The Linq to Xml Application
The application will examine running processes on your computer in three steps.
- In the first step, you will make an XML-document containing all running processes. You will write the XML-document to a file for later use.
- In the second step, you will make an XML-element, containing all running processes, and loop through the sub-elements using Linq.
- In the final step, you will use the XML-document from step 1, reading the XML-file back into a variable. Then you willl use Linq to Xml to select the titles of all processes which are not empty.
You can download all code here.
The application looks like the folllowing:

The command buttons are called (top to bottom): cmdProcs, cmdElements and cmdTitles. The textboxes are called (top to bottom): txtProcs, txtElements and txtTitles.
Clicking a button will execute one of the described steps. The result is shown in the corresponding textbox.
Step 1: XML containing all running processes.
To make an XML-document, containing all running processes, you do the following:
Private Sub cmdProcs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcs.Click
Dim XmlProcs = <?xml version="1.0"?> <myProcesses> <%= From proc In System.Diagnostics.Process.GetProcesses Select <process id=<%= proc.Id %>> <name><%= proc.ProcessName %></name> <title><%= proc.MainWindowTitle %></title> <threads><%= proc.Threads.Count %></threads> </process> %> </myProcesses>
txtProcs.Text = XmlProcs.ToString
My.Computer.FileSystem.WriteAllText("test.xml", xmlProcs.ToString, False)
End Sub
You could leave the <?xml version="1.0">. With this first tag, you create an Xml-Document. Without it, you create an XML-element.
Notice that in VB10 you can leave the underscores at the end of a line if the command spans multiple lines.
Step 2: Looping through XML using Linq
Private Sub cmdElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdElements.Click
Dim xmlProcs = <myProcesses> <%= From proc In System.Diagnostics.Process.GetProcesses Select <process id=<%= proc.Id %>> <name><%= proc.ProcessName %></name> <title><%= proc.MainWindowTitle %></title> <threads><%= proc.Threads.Count %></threads> </process> %> </myProcesses>
Dim xmlProcs2 = From proc In xmlProcs...<process> Select proc.<name>.Value, proc.@id
For Each item In xmlProcs2 txtElements.Text &= item.id & " - " & item.name & vbNewLine Next
End Sub
In XmlProcs you build an XML-element, containg information about the running processes. XmlProcs2 selects two subelements from all <process>-elements from xmlProcs. This way, you can loop through all items in xmlProc2. Notice that you can address the Name and Id of each element using an object-notation. This is due to the Linq to Xml functionality you use in the selction statement for xmlProc2.
Step 3: Load XML from file. Use Linq to make selections in the XML-file.
Private Sub cmdTitles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTitles.Click
Dim xmlFromFile = XDocument.Load("test.xml")
Dim NonEmpty = <NonEmptyProcesses> <%= From Elms In xmlFromFile...<process> Where Elms.<title>.Value <> "" Select Elms %> </NonEmptyProcesses>
For Each item In NonEmpty...<process> txtTitles.Text &= item.<title>.Value & vbNewLine Next
End Sub
The XDocument.Load fuunction loads the xml file from step 1 into xmlFromFile variable. The Linq expression selects all elements having a non-empty title. In the For-Each loop the result of this selection are displayed in the textbox.
|