An XML File as a Text-Based Document |
|
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. 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: Introducing XML
|
|
- Start Notepad and, in the empty document, type the following:
Imports System
Imports System.xml
Module Exercise
Public Sub Main()
Console.WriteLine("Introduction to XML")
End Sub
End Module
|
- Save the file in a new folder named IntroXML inside your
VBasic folder
- Save the file itself as exercise.vb in the above IntroXML
folder
- Open the Command Prompt and Change the Directory to your IntroXML
folder
- To compile the exercise, type vbc exercise.vb and press Enter
- To execute the exercise, type exercise and press Enter
- Return to Notepad
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:
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 |
|
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 Overridable Sub LoadXml(ByVal xml As String)
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:
Overloads Public Overridable Sub Save(ByVal filename As String)
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: Creating an XML Document
|
|
- To start with XML, change the file as
follows:
Imports System
Imports System.xml
Module Exercise
Public Sub Main()
Dim docXML As XmlDocument = New XmlDocument
docXML.LoadXml("")
End Sub
End Module
|
- Save the file
|
|