XML
C#
Programming
Data Parsing
Software Development

How to deal with XML in C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, there are several good ways to work with XML, and the right one depends on whether you need fast streaming reads, easy in-memory editing, or object serialization. The core decision is usually between XmlReader, XDocument, and XmlSerializer.

Use XDocument for Comfortable In-Memory XML Work

If readability and ease of modification matter, XDocument is often the best starting point.

csharp
1using System;
2using System.Xml.Linq;
3
4var doc = XDocument.Parse(@"
5<users>
6  <user id='1'><name>Ada</name></user>
7  <user id='2'><name>Linus</name></user>
8</users>");
9
10foreach (var user in doc.Root!.Elements("user"))
11{
12    Console.WriteLine(user.Element("name")?.Value);
13}

This API is concise and works well for configuration files, moderate-size documents, and transformation tasks.

Use XmlReader for Large or Streaming Documents

If the XML is large or you need efficient forward-only reading, XmlReader is the better tool.

csharp
1using System;
2using System.Xml;
3
4using var reader = XmlReader.Create("users.xml");
5while (reader.Read())
6{
7    if (reader.NodeType == XmlNodeType.Element && reader.Name == "user")
8    {
9        Console.WriteLine(reader.GetAttribute("id"));
10    }
11}

XmlReader is lower-level and less convenient than XDocument, but it avoids loading the entire document into memory.

Use XmlSerializer for Object Mapping

If the XML structure maps cleanly to classes, serialization can simplify the code.

csharp
1using System;
2using System.IO;
3using System.Xml.Serialization;
4
5[XmlRoot("user")]
6public class User
7{
8    [XmlAttribute("id")]
9    public int Id { get; set; }
10
11    [XmlElement("name")]
12    public string? Name { get; set; }
13}
14
15var serializer = new XmlSerializer(typeof(User));
16using var reader = new StringReader("<user id='1'><name>Ada</name></user>");
17var user = (User)serializer.Deserialize(reader)!;
18Console.WriteLine(user.Name);

This approach is excellent when the XML schema is known and stable.

Choose the API by Document Shape and Workflow

A practical rule is:

  • use XDocument for easy querying and editing
  • use XmlReader for large streaming workloads
  • use XmlSerializer for class-based mapping

The APIs are not competitors so much as tools for different XML workloads.

Common Pitfalls

  • Loading huge XML files into memory when a streaming reader would be safer.
  • Using low-level reader code when XDocument would make the logic much clearer.
  • Expecting XmlSerializer to be convenient when the XML shape is irregular or unstable.
  • Ignoring namespaces, which often explain why XML queries seem to return nothing.
  • Treating XML work as one generic task instead of choosing the API based on the real requirement.

Summary

  • In C#, the main XML tools are XDocument, XmlReader, and XmlSerializer.
  • 'XDocument is great for readable in-memory manipulation.'
  • 'XmlReader is the right tool for streaming and large documents.'
  • 'XmlSerializer is useful when XML maps naturally to classes.'
  • The best XML strategy depends on the document size, shape, and workflow.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.