XML (Extensible Markup Language)
XML is a markup language for structured data: elements with names of your own choosing, nested inside each other. In .NET it is in every project file, even though JSON is more common for data today.
XML (Extensible Markup Language) is a markup language in which data stands as a tree of elements with names of your own choosing. An element starts with <Name> and ends with </Name>, and in between there is text or further elements. Additional information stands as attributes in the start tag. There is exactly one root element.
XML in .NET
You meet XML in .NET sooner than you might think: every .csproj file is XML. Its root element Project names the SDK through the attribute Sdk, and settings and package references stand in it as nested elements:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Example.Package" Version="1.0.0" />
</ItemGroup>
</Project>
Directory.Build.props, .resx resource files, and many older configuration files are XML too. For application data and configuration, on the other hand, JSON has won out in .NET, and communication between services usually runs over REST with JSON. XML stays important for formats that are defined as XML: Office documents, feeds, sitemaps, certain government and invoice formats, and older SOAP services.
Reading and writing XML in code
.NET knows three models for XML in memory: the class XmlDocument (after the W3C document object model), XPathDocument (read-only, for XPath), and LINQ to XML. For new code, LINQ to XML is the usual way. You work with XElement and XDocument and query with LINQ:
using System.Xml.Linq;
var doc = XDocument.Parse("""
<Project>
<Package Name="Alpha" Version="1.0.0" />
<Package Name="Beta" Version="2.1.0" />
</Project>
""");
var names = doc.Root!
.Elements("Package")
.Select(p => (string?)p.Attribute("Name"))
.ToList();
doc.Root is the root element. With XDocument, the query methods refer to the document itself, which is why you go through Root first. If you do not need a document in the narrower sense, XElement is enough.
Misconceptions
- XML is not a programming language and not a data format for everything. It describes structure, not behavior.
- XML is not the same as HTML. HTML has fixed elements and forgives errors; XML has names of your own choosing and demands well-formed documents. For a comparison, see HTML.
- XML is wordy. In many cases that speaks for JSON, but not where a standard prescribes XML.
- Outside XML is input like any other. Check its size and content before you trust it.
An example from the Kladde
The Kladde imports tenders from the EU's public TED interface. The import reads no XML files: the request goes to the interface as JSON, the answer comes back as JSON and is read with ReadFromJsonAsync. In the course, you meet XML where .NET itself uses it: in the Kladde's project file, whose structure the first lesson in K1 explains line by line.
In the course
You get to know the project file as XML in K1, module 1.1, in the lesson From a file-based app to a project. The format the Kladde uses for data instead is JSON: JSON in module 1.3. Handling XML data in C# is not a teaching topic of its own.