XML annotations in Visual Studio.NET (1)

Source: Internet
Author: User
Tags command line comments xsl xsl file visual studio
Visual|xml XML annotations in Visual Studio.NET10/18/2001 11:1:33 Yesky

   introduce

In this practice, you will insert an XML annotation in the C # source file and then generate the XML file through the C # compiler. You can use XML-related annotation tokens in C # source code to indicate which annotations need to be exported to an XML file.

The XML file can be used as a code document. You can add new annotations to existing files, or modify existing ones to keep the document consistent with its corresponding code. In order to create an updated document record, you have to regenerate the XML file.

In this exercise, you will:

· Create and edit a project file using the Microsoft Visual Studio.NET integrated development environment

· Create an XML file (doc.xml)

· Open projects and source files in the IDE

· Compiling projects with MS-DOS commands

· Applying an XSL template to an XML file

· Open Doc.xml final version

   Create a new project in the IDE

1. Click Start, click Programs, click Microsoft Visual studio.net7.0, and then click Microsoft Visual Studio. NET 7.0. The IDE startup page is displayed, as shown in Figure 1.


Figure 1 Visual Studio.NET IDE startup page
Attention:

When you start the IDE in this way, the default startup page opens. It provides links to the last four projects in this working environment, as well as links to other resources.

You can change your default settings. Click Tools, click Options, and then choose the settings you like.

2. Click File, click New, and then click Project. The New Project window appears, as shown in Figure 2.

3. Click Visual C # Projects in the Project types panel and click Empty Project on the Templates panel.

4. In the Name field, type the XML Comments, and then select the location where your project is stored (we use the default location in this operation).

Note: In this project name, there is a space between "XML" and "Comments".

5. Click OK.


Figure 2. New Project Window creating XMLComment.cs Source Files

The XMLComment.cs file is the source file used to create the XML document. XML tags and their associated text are commented out in C # code. When this source file is compiled, XML documents are generated from these XML tags and text.

Attention:

XML annotation tags are new to C #. Preceded by a triple slash (///), it tells the compiler that the text following the slash is an XML annotation. We must start with a triple slash to declare this behavior XML comment line. XML annotations must be in the format of an XML tag in order to generate an XML document.

1. Click File, and then click Add New Item. The Add New item window will be displayed as shown in Figure 3.

2. Click Local Project Items in the Categories Panel and click Code File in the Templates panel.

3. Type XMLComment.cs in the Name field.

Note: There are no spaces in the middle of "XMLComment.cs".


Figure 3. ADD New Item window

4. Click Open.

5. In the page, typing the following code:

Using System;

Class XmlElement
{
///
Returns the attribute with the given name and
Namespace
///
The name of the attribute
///
The namespace of the attribute, or null if
The attribute has no namespace
///
The attribute value, or null if the attribute
does not exist
///
public string GetAttr (string name, string ns) {
return "Test";
}
}
6. Click File, then click Save XMLComments.cs (or click on the Save icon). The file is stored in your project folder.

7. Click File, and then click Exit. If the Microsoft Development window appears, click Yes to save your project.

   open projects and source files in the IDE

1. Click Start, click Programs, click Microsoft Visual studio.net7.0, and then click Microsoft Visual Studio. NET 7.0.

2. Click File, click Open, and then click Project.

3. Open path C:\Documents and Settings\Administrator\My Documents\Visual Studio projects\xmlcomments

4. Click Xmlcomments.csproj or XMLComments.sln, and then click Open.

Note: Click on any of the two files to open the project

If the source file is not open in the main window, double-click XMLComments.cs in the Solution Explorer window. (see Figure 4). The source file opens in the main window. Keep the IDE open and Y to complete the steps below.


Figure 4. XMLComment.cs the document's view in the IDE
Note: The System namespace (namespaces) is declared at the top of the file. So all the classes in the namespace are available in the XmlElement class.

The XML element "param" has two properties, name, and NS. We pass these property values as arguments to GetAttr ().

   compiling

In this chapter, you will use two existing files, Make.bat and doc.xsl. These files must be stored in the Xmlcomments project folder before the project can be compiled correctly.

Note: The doc.xsl file provides a type table template for displaying doc.xml files in a Web browser. The Make.bat file contains C # compilation options.

1. Click Start, click Programs, click Accessories, and then click Windows Exployer.

2. Open the path C:\FieldContent\Experience vs.net\lab2.

3. Highlight the files doc.xsl and Make.bat, click Edit, and then click Copy.

4. Open the path C:\Documents and Settings\Administrator\My documents\visual Studio projects\xmlcomments.

5. Click Edit, and then click Paste to paste the file into the Xmlcomments folder. You will use the MS-DOS command line to compile this project.

6. In Windows Explorer, open the path C:\FieldContent\Experience vs.net\lab2.

7. Double-click Command Prompt.

8. Type Make.bat, and then press ENTER. The contents of the Make.bat file are displayed on the screen.

Note: The Make.bat file contains the following parameters:

The CSC command called the C # compiler

/target option specifies the format of the output file. In this case, it is a library, a DLL (XMLComment.dll).

The/doc option tells the compiler to create an XML file (Doc.xml) from a document comment in the source file

XMLComment.cs is the source file that you want to compile.

9. Type make, and then press ENTER. The command Line Prompt window appears as shown in Figure 5.

Note: After compiling with the Make command, the Doc.xml XMLComment.dll file is created. And they are automatically stored in the Xmlcommand project folder. For C #, this is the default. You don't have to specify the storage path unless you want to change it.

Figure 5. DOS Command window after generating doc.xml

10. Closes the command prompt.

Doc.xsl's download Address: http://msdn.microsoft.com/code/default.asp?url=/code/sample.asp?url=/msdn-files/027/001/686/ Msdncompositedoc.xml

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.