Skip to content
Tutorials Center
My learning

Topics

Lesson 119 of 182 ยท Standard Library

XML, CSV and Other Formats

Learn to work with XML using LINQ to XML and XmlSerializer, write and parse CSV by hand, encode Base64 and hex, use UTF-8 helpers and parse URIs in C#.

JSON gets the attention, but real programs meet many other formats: XML in configuration files, SOAP services, Office documents and RSS feeds; CSV exports from spreadsheets; Base64 in tokens and e-mail; hexadecimal in hashes; and URLs everywhere. The .NET base library handles all of these without extra packages.

This lesson covers the tools you will reach for most: LINQ to XML (XDocument) for reading and building XML, XmlSerializer for mapping classes to XML, a careful CSV writer and parser, Base64 and hex conversions, the UTF-8 helpers, and Uri with its builder. For JSON see the previous lesson.

XML in a minute

XML stores data as a tree of elements with attributes and text:

XML
<library>
  <book id="1" available="true">
    <title>Dune</title>
    <price currency="USD">9.99</price>
  </book>
</library>

Rules: exactly one root element, every opened tag is closed, attribute values are quoted, names are case-sensitive, and the characters <, >, &, " and ' must be escaped inside text and attributes (&lt;, &gt;, &amp;, &quot;, &apos;). The classes below do the escaping for you.

LINQ to XML: building

In System.Xml.Linq, XElement is an element, XAttribute an attribute and XDocument the whole document. Constructors take content as parameters, so the C# code mirrors the shape of the XML (this is called functional construction):

C# Building XML with XElement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

ToString() produces indented XML without the declaration; doc.Save(path) or doc.Save(stream) writes the declaration too. Notice that "Q&A <draft>" was escaped for you, which is one more reason not to build XML by gluing strings together: a stray & makes the file invalid and an attacker-controlled < lets someone inject elements.

You can also generate XML from data with a LINQ query inside the constructor:

C# Generating XML from a collection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

LINQ to XML: querying

Parse text with XDocument.Parse (or XDocument.Load for files and streams). Then Element, Elements, Attribute, Descendants and casts do the work. Casting an XElement or XAttribute to int, decimal, bool, DateTime or string converts the text, and casting to a nullable type gives null for a missing node instead of throwing:

C# Querying XML

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Pitfall: book.Element("title") returns null when absent, so book.Element("title").Value throws NullReferenceException. Use the cast (string?)book.Element("title") or ?.Value. Another trap: Elements() returns only the direct children; use Descendants() for all depths.

XPath

If you prefer XPath, System.Xml.XPath adds XPathSelectElements and XPathEvaluate:

C# XPath on an XDocument

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Modifying XML

The tree is mutable. Change values with SetValue, SetAttributeValue, Add, AddFirst, Remove, ReplaceWith:

C# Editing a tree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

When you remove nodes inside a foreach over the live query, the enumeration breaks. Call .ToList() first, as above.

Namespaces

Real-world XML (SVG, Atom, Office files) uses namespaces. In LINQ to XML the full name is an XName, built with an XNamespace. Forgetting the namespace is the classic reason a query returns nothing:

C# XML namespaces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Safe XML loading

XML parsers can be abused. A hostile document may define huge "entities" (the "billion laughs" attack) or reference external files (XXE). XDocument.Parse and XDocument.Load never fetch external resources, but they do expand entities that the document defines itself. A plain XmlReader.Create is stricter: it prohibits DTDs altogether. For data from outside your control, hand XDocument.Load a reader with DtdProcessing.Prohibit, and never set an XmlResolver or DtdProcessing.Parse. For big files use XmlReader (a forward-only reader) with XElement.ReadFrom to process one record at a time:

C# Prohibiting DTDs for untrusted XML

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

XmlSerializer: classes to XML

XmlSerializer converts objects to XML and back, similar to JsonSerializer. The type needs a public parameterless constructor and public read/write properties. Attributes shape the XML:

C# XmlSerializer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The declaration says utf-16 only because we wrote into a StringWriter; a StreamWriter or file stream gives utf-8. Compared with LINQ to XML, XmlSerializer is convenient for stable, schema-like documents, but it is rigid: no dictionaries, no private members, the classes must be public, and the first use is slow because the serializer generates a helper assembly. Use LINQ to XML when you need to read part of a document, tolerate variations, or edit in place. Two other XML tools exist for specific cases: DataContractSerializer (WCF-style contracts) and XmlDocument (the older DOM, still found in legacy code).

Writing CSV by hand

CSV (comma-separated values) looks trivial and is a minefield. Fields containing a comma, a quote or a newline must be wrapped in double quotes, and a literal quote inside is doubled. There is no CSV class in the base library, so you write a small writer and parser. Here is the writer:

C# A correct CSV writer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Notes: the standard (RFC 4180) line ending is CRLF; numbers must be written with the invariant culture or a German machine would write 95,5 and corrupt the file; and strings starting with =, +, - or @ can run as formulas when someone opens the file in a spreadsheet (CSV injection), so prefix them with ' when the data is untrusted.

Parsing CSV

Splitting on commas fails as soon as a field contains a comma. A tiny state machine handles quotes, doubled quotes and embedded newlines:

C# A small CSV parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

For production code that reads user-supplied CSV, consider a tested NuGet library (CsvHelper is the best known; not usable in our sandbox, so this is read-only advice). The hand-written version above is fine for files you control.

Base64 and hex

Binary data must sometimes travel through text-only channels (JSON, e-mail, URLs). Base64 encodes every 3 bytes as 4 characters from A-Z a-z 0-9 + /, padded with =. Hex writes each byte as two digits:

C# Base64 and hex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Base64 and hex are encodings, not encryption. Anyone can decode them. Never use them to "hide" passwords or secrets.

Base64Url (since .NET 9) is the format used in JWTs and URL tokens. Before .NET 9 you replaced + and / by hand.

UTF-8 helpers

Text on disk and on the wire is mostly UTF-8. Encoding.UTF8 converts between strings and bytes, and the u8 literal creates a UTF-8 byte span at compile time with no allocation:

C# UTF-8 conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Characters outside ASCII take 2 to 4 bytes in UTF-8, so string.Length (UTF-16 code units) and byte counts differ. Always allocate buffers from GetByteCount, never from Length. The full story is in Unicode and Encoding.

Uri parsing and building

Uri parses a URL into parts and validates it. UriBuilder and Uri.EscapeDataString build URLs safely:

C# Uri and UriBuilder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Rules of thumb:

  • Parse with Uri.TryCreate for user input; new Uri(text) throws UriFormatException.
  • Escape each component with Uri.EscapeDataString; do not escape a whole URL.
  • Check the Scheme (https/http) before fetching a user-supplied URL, otherwise file: or internal addresses could be requested (server-side request forgery).
  • Uri equality ignores the fragment in some comparisons and is case-insensitive for scheme and host, so compare with care.

Choosing a format

Need Good choice
Web APIs, config, general data JSON
Documents with mixed content, namespaces, legacy systems XML
Simple tables exchanged with spreadsheets CSV
Binary inside text Base64 (or Base64Url in URLs)
Compact, fast, private binary data BinaryWriter or a binary serializer library
Hashes and keys for humans to read Hex

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does book.Element("title") return when the <title> child is missing?

    Element returns null for a missing child. Use a cast to a nullable type such as (string?)book.Element("title") to read safely.
  2. 2. Your query root.Elements("entry") returns nothing for an Atom feed that clearly contains entries. What is the likely cause?

    With xmlns="..." every element lives in that namespace, so the name must include the XNamespace.
  3. 3. How should a CSV field containing He said "hi" be written?

    The field is wrapped in double quotes and each inner double quote is doubled.
  4. 4. Which statement about Base64 is true?

    Base64 is a reversible encoding for carrying bytes through text channels; it adds roughly 33% overhead and offers no secrecy.
  5. 5. Why use Uri.EscapeDataString(term) when putting user input into a query string?

    Unescaped & or # would end the value or start a fragment. Escaping each component keeps the URL structure intact.

Summary

  • LINQ to XML (XDocument, XElement, XAttribute) builds, queries and edits XML; casts to nullable types avoid NullReferenceException; remember namespaces via XNamespace.
  • Keep DTD processing off for untrusted XML; stream huge files with XmlReader.
  • XmlSerializer maps public classes to XML with attributes like [XmlAttribute], [XmlElement] and [XmlArray].
  • CSV needs quoting for commas, quotes and newlines; write with the invariant culture and use a state machine (or a library) to parse.
  • Convert handles Base64 and hex; Base64Url is for tokens in URLs; neither is encryption.
  • Encoding.UTF8 and u8 literals convert text and bytes; size buffers with GetByteCount.
  • Uri, UriBuilder and Uri.EscapeDataString parse, build and escape URLs; validate the scheme of untrusted URLs.

Next: HttpClient