How to use xml without LINQ in C# the old school way.

Contents


XML History Segment (For the nerds)

History of XML, the eXtremely Messy Language that we all use and abuse. XML was born in 1996 as a way to structure and store data in a human-readable and machine-friendly format. It was inspired by SGML, the Super Gargantuan Markup Language, which was used for documents like HTML. XML was supposed to be simpler and more flexible than SGML, but it still had some quirks and limitations. For example, to create an XML document in C#, you had to write something like this:



That's five lines of code just to create a simple XML document with one element and one attribute! And that's not even the worst part. To parse and query XML data in C#, you had to use classes like XmlReader, XmlWriter, XPathNavigator, XslCompiledTransform, and so on. It was like trying to eat soup with a fork!


But then, in 2007, Microsoft introduced LINQ, the Language Integrated Query, which changed everything. LINQ is a set of features that allow you to write queries directly in C#, using a syntax that resembles SQL. With LINQ, you can create and manipulate XML documents in a much easier and elegant way. For example, to create the same XML document as before, you can write this:


That's one line of code! And to query the XML data, you can use methods like Element, Attribute, Descendants, Where, Select, etc. It's like eating soup with a spoon!


So that's XML and how it evolved from a complex and verbose language to a simple and expressive one.

Using XML the old fashion way

Greetings, fellow masochists! Today I'm going to show you how to use XML in C# without modern LINQ, explaining and demonstrating it through CRUD operations. Why would you want to do that, you ask? Well, maybe you're feeling nostalgic for the old days of XPath and XDocument, or maybe you just hate yourself and want to suffer. Either way, I'm here to make it worse!

Create XML file

First, let's create a simple XML file that represents a garden with different vegetables. We'll call it garden.xml and save it in our project folder. Here's what it looks like:


Isn't it beautiful? So simple and elegant. Too bad we're going to ruin it with some ugly C# code.

Load Document

Now, let's say we want to perform some CRUD operations on this XML file using C#. How do we do that? Well, we need to load the XML file into an XmlDocument object, which allows us to manipulate the XML tree using methods and properties. Here's how we do that:

 

Easy enough, right? Now we have access to the root element of the XML file, which is Garden, and its child elements, which are Vegetable. We can use the SelectNodes method to get a collection of nodes that match an XPath expression, or the SelectSingleNode method to get a single node that matches an XPath expression. For example, if we want to get all the vegetables in the garden, we can do this:


Or if we want to get a specific vegetable by its id attribute, we can do this:


Now that we know how to select nodes from the XML file, let's see how we can create, update and delete them.

Create node

To create a new node, we need to use the CreateElement method of the XmlDocument object, which takes the name of the element as a parameter and returns an XmlElement object. We can then set the attributes and inner text of the element using the SetAttribute and InnerText properties. For example, if we want to add a new vegetable called Cucumber with id 4 and color Green, we can do this:


Now we have created a new XmlElement object that represents our new vegetable, but it's not yet part of the XML file. To add it to the XML file, we need to use the AppendChild method of the parent node, which in this case is Garden. We can get the Garden node using the DocumentElement property of the XmlDocument object. Here's how we do that:

Save to file

Now our new vegetable is part of the XML file. To save the changes to the file, we need to use the Save method of the XmlDocument object, which takes the file name as a parameter. Here's how we do that:



And that's how we create a new node in an XML file using C#. Pretty simple, right? Well, not really. Imagine if we had to create multiple nodes with different attributes and values. We would have to write a lot of repetitive code for each node. That's why modern LINQ is so much better for working with XML files. It allows us to create and manipulate XML trees using a declarative syntax that is much more concise and readable.

But I digress. Let's move on.

Updating Nodes

To update a node in an XML file using C#, we need to first select the node we want to update using the SelectSingleNode method, as we did before. Then we can modify the attributes and inner text of the node using the SetAttribute and InnerText properties. For example, if we want to change the color of the Tomato vegetable from Red to Yellow, we can do this:




Notice how we used the indexer syntax to access the child element of the Tomato node by its name. This is a shortcut for using the SelectSingleNode method with an XPath expression. We can also use the Attributes property to access the attributes of the node by their name. For example, if we want to change the id of the Potato vegetable from 3 to 5, we can do this:


After updating the nodes, we need to save the changes to the file using the Save method, as we did before.

And that's how we update nodes in an XML file using C#. Not too hard, right? Well, not really. Imagine if we had to update multiple nodes with different attributes and values. We would have to write a lot of repetitive code for each node. That's why modern LINQ is so much better for working with XML files. It allows us to query and modify XML trees using a fluent syntax that is much more expressive and flexible.

But I digress. Let's move on.

Deleting Nodes

To delete a node in an XML file using C#, we need to first select the node we want to delete using the SelectSingleNode method, as we did before. Then we need to use the RemoveChild method of the parent node, which takes the node to be removed as a parameter. For example, if we want to delete the Cucumber vegetable that we added before, we can do this:




After deleting the node, we need to save the changes to the file using the Save method, as we did before.

And that's how we delete nodes in an XML file using C#. Not too complicated, right? Well, not really. Imagine if we had to delete multiple nodes based on some condition. We would have to write a lot of repetitive code for each node. That's why modern LINQ is so much better for working with XML files. It allows us to filter and remove XML nodes using a functional syntax that is much more concise and powerful.

Let's wrap up this post.

As you can see, using XML in C# without modern LINQ is possible, but not very fun. It requires a lot of boilerplate code that is verbose and tedious to write and read. It also makes it harder to perform complex operations on XML trees, such as filtering, sorting, grouping, transforming, etc.

That's why I recommend you to use modern LINQ whenever you need to work with XML files in C#. It will make your life much easier and your code much cleaner and more elegant.

But hey, if you're feeling adventurous and want to challenge yourself, go ahead and try using XML in C# without modern LINQ. Just don't blame me if you end up crying or smashing your keyboard.

I hope you enjoyed this post and learned something new. If you have any questions or comments, feel free to leave them below. And don't forget to subscribe to my blog for more posts like this one.

Happy coding!

 


Comments