Working with XML Using C#

Jawadhasan
3 min readDec 17, 2021

Introduction

The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more.

It is a very common format and In this post, we will learn some of the basic ways to work with XML in C#.

Getting Started

XDocument and XElement are the the main classes for dealing with XML in .NET .

  • XDocument represents a complete valid XML document.
  • XElement represents an XML element or elements.

LINQ to XML queries are also very popular when working with XML.

For demos, I’ve created a new project using .NET Core Console Template. I’ve also setup a git repository for the C# code which we will see in this post.

Build XML in Code

Lets see a very basic example of creating XML in code:

As you can see that it is very easy to create XML using XDocument constructor. We can add declarations, comments, elements, attributes etc.

--

--

Jawadhasan

Software Solutions Team Lead | Cloud Architect | Solutions Architect