Home

Creating an XML File

 

An XML File as a Text-Based Document

 

Introduction

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

Many other applications allow creating an XML file or generating one from an existing file. There are also commercial editors you can get or purchase to create the XML file. You can also create your own XML editor using 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.

 

Practical Learning Practical Learning: Introducing XML

  1. Start Notepad and, in the empty document, type the following:
     
    // Project Name: IntroXML
    // Purpose:      Introduction to XML
    
    using System;
    
    namespace IntroXML
    {
        class Exercise
        {
            static int Main()
            {
    	    Console.WriteLine("Introduction to XML");
    
    	    return 0;
            }
        }
    }
  2. Save the file in a new folder named IntroXML inside your CSharp Lessons folder
  3. Save the file itself as exercise.cs in the above IntroXML folder
     
  4. Open the Command Prompt and Change the Directory to your IntroXML folder
  5. To compile the exercise, type csc exercise.cs and press Enter
  6. To execute the exercise, type exercise and press Enter
     
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\Documents and Settings\Administrator>CD\
    
    C:\>CD CSharp Lessons\IntroXML
    
    C:\CSharp Lessons\IntroXML>csc exercise.cs
    Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
    for Microsoft (R) .NET Framework version 1.1.4322
    Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
    
    
    C:\CSharp Lessons\IntroXML>exercise
    Introduction to XML
    
    C:\CSharp Lessons\IntroXML>
  7. Return to Notepad

Creating an XML File

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. 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:

Save As

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

 

The Document Object Model and the XmlDocument

 

Introduction

To implement XML, the .NET Framework provides the System.Xml namespace. When you create an XML file, there are standard rules you should (must) follow in order to have a valid document. The standards for an XML file are defined by the W3C Document Object Model (DOM). To support these standards, the System.Xml namespace provides the XmlDocument class. This class allows you to create an XML document, its contents, and many other related operations you may want to perform on the contents of the file.

Creating XML Code Using XmlDocument

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

public virtual void LoadXml(string xml);

This method takes a string 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. You can also first declare and initialize a string variable with the XML code, then pass it as argument to the XmlDocument.LoadXml() method.

If you use the XmlDocument.LoadXml() method, only the XML code is created, not the file. To actually create the Windows file, you can call the XmlDocument.Save() method. This method is provided in four versions. One of the versions takes as argument a string value that would be the file name. The syntax of this method is:

public virtual void Save(string filename);

The argument must be a valid filename and must include the .xml extension. If you pass a string without backlashes, the file would be created in the same folder as the current project. If you want the file to be created somewhere else (in a different directory), pass the whole path. 

 

Practical Learning Practical Learning: Creating an XML Document

  1. To start with XML, change the file as follows:
     
    // Project Name: IntroXML
    // Purpose:      Introduction to XML
    
    using System;
    using System.Xml;
    
    namespace IntroXML
    {
        class Exercise
        {
            static int Main()
            {
    	    XmlDocument docXML = new XmlDocument();
    			
    	    docXML.LoadXml("");
    
    	    return 0;
            }
        }
    }
  2. Save the file
 

Previous Copyright © 2005-2016, FunctionX Next