What an XML file is and why you might create one

An XML file is a text document that stores data in a structured format using tags — similar to HTML, but designed to describe what the data means rather than how it looks. You create an XML file when you need to store information in a way that other programs can read and understand, like inventory lists, configuration settings, contact records, or data exports.

XML files are plain text, so you can create one in any text editor. The file itself is just a .xml extension attached to a document that follows XML rules: opening and closing tags, properly nested elements, and a declaration at the top that identifies it as XML. You do not need special software — a basic text editor and knowledge of the format is enough.

Key Takeaways

  • XML files are plain text documents you can create in Notepad, TextEdit, or any text editor by typing tags that describe your data.
  • Every XML file must start with a declaration line like <?xml version="1.0"?> and have one root element that contains all other elements.
  • Tags must open and close properly, nest inside each other without overlapping, and follow naming rules that start with a letter or underscore.
  • Save your file with a .xml extension, and you can open it in a browser or text editor to check that the structure is correct.

The basic structure every XML file needs

Every XML file must have three things: a declaration, a root element, and properly nested child elements. The declaration goes on the first line and tells the system this is an XML file. It looks like this: <?xml version="1.0" encoding="UTF-8"?>

After the declaration, you need one root element that wraps everything else. If you are storing a list of books, your root might be <library>. Inside that, you add child elements for each book: <book><title>The Name</title><author>Jane Smith</author></book>. Every opening tag must have a closing tag with a forward slash, and tags cannot overlap — if you open <book> inside <library>, you must close <book> before closing <library>.

Creating an XML file in Notepad on Windows

Open Notepad (search for "Notepad" in the Windows Start menu). Type your XML declaration on the first line, then add your root element and child elements. Here is a simple example for a contact list:

<?xml version="1.0" encoding="UTF-8"?> <contacts>   <contact>     <name>Alice Johnson</name>     <email>alice@example.com</email>     <phone>555-1234</phone>   </contact> </contacts>

Once you have typed your content, go to File > Save As. In the filename field, type a name with the .xml extension — for example, contacts.xml. Make sure the "Save as type" dropdown is set to "All Files" so Windows does not add .txt to your filename. Choose where you want to save it and click Save.

Creating an XML file on Mac using TextEdit

Open TextEdit (find it in Applications > Utilities). Before you type anything, go to Format menu and select "Make Plain Text" — this is important because TextEdit defaults to rich text, which will break your XML file. Then type your XML content using the same structure as the Windows example above.

When you are ready to save, go to File > Save. In the filename field, type your name with .xml at the end, such as contacts.xml. TextEdit will ask if you want to use the .txt extension instead — click "Use .xml" to keep the correct format. Choose your location and save.

Creating an XML file on Linux using the terminal or a text editor

On Linux, you can use any text editor. Open a terminal and type nano myfile.xml (replace myfile with your chosen name). This opens the nano editor. Type your XML content, then press Ctrl+X, then Y, then Enter to save and exit.

Alternatively, open a graphical text editor like gedit or Kate, type your XML, then save the file with a .xml extension through File > Save. Linux does not add extensions automatically, so make sure you type the full filename including .xml.

Rules for naming tags and checking your work

XML tag names must start with a letter or underscore, can contain numbers and hyphens, and are case-sensitive — <Name> and <name> are different tags. Avoid spaces in tag names and do not use special characters like <, >, or & inside tag names. If you need to include those characters in your data, use the codes &lt;, &gt;, and &amp; instead.

To check if your XML file is correct, open it in a web browser by dragging it into Chrome, Firefox, Safari, or Edge. If the structure is valid, the browser will display it as a tree you can expand and collapse. If there is an error, the browser will show you the line number and describe what is wrong — usually a missing closing tag or a tag that is not properly nested.

Frequently Asked Questions

Can I add comments to an XML file?

Yes. Comments in XML use the format <!-- this is a comment -->. They can span multiple lines and are ignored when the file is read by other programs. Use them to explain what sections of your file do or to leave notes for yourself.

What if I need to store special characters like quotes or ampersands?

Use XML entity codes: &quot; for a double quote, &apos; for a single quote, &amp; for an ampersand, &lt; for a less-than sign, and &gt; for a greater-than sign. For example, <title>Tom &amp; Jerry</title> stores the ampersand correctly.

Can I add attributes to XML tags?

Yes. Attributes go inside the opening tag and describe properties of the element. For example, <book isbn="978-0-123456-78-9"><title>My Book</title></book> adds an ISBN attribute to the book element. Attribute values must be in quotes.

What program should I use to edit an XML file after I create it?

Any text editor works — Notepad, TextEdit, gedit, VS Code, or Sublime Text. Some editors like VS Code highlight XML tags in different colors, which makes it easier to spot errors. Do not use Microsoft Word or Google Docs, as they add formatting that breaks XML structure.