You can use the XMLTextWriter class to create XML content that uses a namespace. The code lies used to illustrate the idea are taken from the article How to use XMLTextWriter to create an XML document in C#, and you can find more information about XML namespaces from the article How to use XML Namespaces in XML document.

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development: Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code, 4th Edition
ASP.NET Core 3 and Angular 9: Full stack web development with .NET Core 3.1 and Angular 9, 3rd Edition
C# in Depth, 4th Edition

The XmlTextWriter includes an overloaded version of the WriteStartElement() method that accepts a namespace URI. Here’s how it works:


string ns = “”;


w.WriteStartElement(“BooksList”, ns);

// Write the first book.

w.WriteStartElement(“Book” , ns);