Home

Writing XML Code

 

Introduction to the Document Object Model

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, to populate it with the desired contents, and to perform many other related operations on the contents of the file.

 

Practical Learning Practical Learning: Introducing XML

  1. Start Microsoft Visual C# and create a Console Application named CollegeParkAutoParts1
  2. To save the application, on the Standard toolbar, click the Save All button
  3. Accept all defaults and click Save

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

Practical Learning Practical Learning: Creating an XML Document

  1. To start with XML, change the Program.cs file as follows:
     
    using System;
    using System.Xml;
    
    namespace CollegeParkAutoParts1
    {
        class Program
        {
            static int Main(string[] args)
            {
                XmlDocument docXML = new XmlDocument();
    
                docXML.LoadXml("");
                return 0;
            }
        }
    }
  2. Save the Program.cs file

Writing XML Code Using the Code Editor

In the next sections, we will see how to create an XML file with the Add New Item dialog box. After creating the file and displaying it in the Code Editor, you can start editing it. The Code Editor is equipped to assist you with various options. Whenever you start typing, the editor would display one or more options to you. Here is an example:

When different options are presented to you, you can press the arrow keys to select an option and press Enter. You can also use the mouse to click an option. When typing other XML items as we will learn, the Code Editor is equipped to work in concert with you.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next