Logo

Creating an XML File

 

Introduction

Due to the high level of support of XML in the Microsoft .Net Framework, there are various ways you can create an XML file that can be used in Visual Studio .NET. The most common technique consists of using a simple text editor. In Microsoft Windows, this would be Notepad, or even WordPad. An XML file is first of all a normal text-based document that has a .xml extension. Therefore, however you create it, it must have that extension.

Many other applications allow creating an XML file or generating one from an existing file. There are also commercial editors you can purchase to create the XML file. You can also create your own XML editor using Visual C#. Normally, it is not particularly easy because you would also need to include a parser in the application and you may have to write that parser yourself.

 

Creating an XML File With a Text Editor

Probably the most common way to create an XML file in Microsoft Windows consists of using Notepad or any other text editor. After opening the text editor, you can enter the necessary lines of code. Here is an example:

After creating the file, you must save it. When saving it, you can include the name of the file in double-quotes. Here is an example:

You can also first set the Files Of Type combo box to All Files and then enter the name of the file with the .xml extension

 

Creating an XML File With Visual Studio .NET Without a Project

Microsoft Visual Studio .Net provides its own means of creating an XML file. Like any other XML file, Visual Studio doesn't control what you do with the file nor does it control where and when you can use the file. Whether you are working on a project or not, you can start creating an XML file any time.

If you are not working on any project, for example if you had just started Visual Studio and no project is opened, to create an XML file, on the main menu, you can click File -> New -> File... In the Templates section, you can click XML File:

After clicking Open, a new file named XMLFile1 would be created. Internally, the file would have the .xml extension. If you add another XML file using the same technique, it would be called XMLFile2 or the incremental number.

 

Creating an XML File With Visual Studio .NET Without a Project

If you are already working on a project, to start an XML file, on the main menu, you can click Project -> Add New Item... Then, in the Templates section, click XML File (.xml). You would be required to give a name to the file:

After typing a name in the Name box, you can click Open.

A newly added XML file in Visual Studio .Net has a line of code as:

<?xml version="1.0" encoding="utf-8"?>

Under this file, you can add the necessary elements and nodes. Here is an example:

 

Creating XML Code Using XmlDocument

The Microsoft .Net Framework provides the XmlDocument class that you can use to create XML code. This technique allows you to create the contents of a possible XML file as one string and use it as you wish.

To create XML code using XmlDocument, this class has a method called LoadXml(). Its syntax is simply:

public virtual void LoadXml(string xml);

This method takes a String value as argument. The XmlDocument::LoadXml() method doesn't create an XML file, it only allows you to provide or create XML code. The code can be created as argument. Here is an example:

private void btnGenerate_Click(object sender, System.EventArgs e)
{
                   XmlDocument XDoc = new XmlDocument();

                    Doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
		         "<employees><employee><name>Sylvie Aronson</name>" +
		         "<department>Accounting</department></employee>" +
		          "<employee><name>Bertrand Yamaguchi</name>" +
		          "<department>Corporate</department>" +
		          "</employee></employees>");
}

You can also first declare and initialize a String variable with the XML code, then pass the variable as argument to the XmlDocument::LoadXml() method.

After creating the file, you can save it. The XmlDocument class provides the Save() method that comes in four overloaded versions. The simplest takes as argument the name of the file to save to. Its syntax is:

public virtual void Save(string filename);

Here is an example:

private void btnGenerate_Click(object sender, System.EventArgs e)
		{
			XmlDocument XDoc = new XmlDocument();

			XDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				        "<employees><employee><name>Sylvie Aronson</name>" +
		  		        "<department>Accounting</department></employee>" +
		                                        "<employee><name>Bertrand Yamaguchi</name>" +
		                 	        "<department>Corporate</department>" +
		                 	        "</employee></employees>");
			
			XDoc.Save("emplrecords.xml");
		}

Like all other XML file, after creating one with XDocument, you can use it as you wish. Here is the above file viewed in the MyIE2 browser:

 

Home Copyright © 2004-2010 FunctionX, Inc.