Sie sind auf Seite 1von 6

XML is a platform independent language, so the information formatted in XML can be used in any other platforms (Operating Systems).

If we create an XML file in one platform it can be used in other platforms also. For creating a new XML file in VB.NET, we are using XmlTextWriter class . The class takes FileName and Encoding as argument. Also we are here passing formating details . The following source code creating an XML file product.xml and add four rows in the file. XML is a self describing language and it gives the data as well as the rules to extract what the data it contains. Reading an XML file means that we are reading the information embedded in XML tags in an XML file. In the previous program we create an XML file and named it as products.xml. The following program read that file and extract the contents inside the XML tag. We can read an XML file in several ways depends on our requirement. This program read the content in Node wise . Here we are using XmlDataDocument class to read the XML file . In this program it search the Node < Product > and its child Nodes and extract the data in child nodes. Introduction The Extensible Markup Language or XML is a technique of using a document, such as a text file, to describe information and making that information available to whatever and whoever can take advantage of it. The description is done so the document can be created by one person or company and used by another person or another company without having to know who first created the document or how it works. This is because the document thus created is not a program, it is not an application: it is just a text-based document. Because XML is very flexible, it can be used in regular Windows applications, in databases, in web-based systems (Internet), in communication applications, in computer networks, in scientific applications, etc. To make sure that XML can be universally used without one person or group owning it, it is standardized by the W3C organization. XML is released through an XML Recommendation document with a version. To specify the encoding you are using, type encoding followed by the encoding scheme you are using, which must be assigned as a string. The encoding is specified in the the first line. Here is an example:
<?xml version="1.0" encoding="utf-8"?>

Creating an XML File Techniques 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 would be used in Visual Basic .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 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 Visual Basic .NET. 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 Microsoft Visual Studio .NET and create a Windows Application named CPAP1

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. 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 Within 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. Practical Learning: Adding a New XML File 1. To add an XML file, on the main menu, click File -> New -> File... 2. In the Templates list of the New File dialog box, click XML File (.xml)

3. Click Open 4. To save the file, on the the Standard toolbar, click the Save button 5. Locate the folder that contains the current project. Double-click its bin folder to display it in the Save In combo box 6. Change the file name to Parts 7. Click Save 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 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 pointer to String with the XML code, then pass it as argument to the XmlDocument.LoadXml() method. The advantage of creating an XML file using Visual Studio .NET is that the file, its name, and an extension are created automatically. 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 them 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, pass the whole path. Practical Learning: Creating an XML Document 1. 2. 3. 4. Display the form. In the Toolbox, click the Button control and click the form Change its Text to Create Employees and its Name to btnCreateEmployees Double-click the newly added button to generate its Click event In the top section of the file, under the other using lines, type:

using System.Xml; 5. Implement the Click event of the btnCreateEmployees button as follows:

Private Sub btnCreateEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateEmployees.Click Dim docXML As XmlDocument = New XmlDataDocument docXML.LoadXml("") End Sub

6. Save all

Previous

Copyright 2004-2010 FunctionX, Inc.

Next

Das könnte Ihnen auch gefallen