Visual C# .NET provides tools for working directly with XML. At times, you’ll find that you don’t want to write a Visual C# .NET program to create an XML file—instead, you simply need an XML file to use in your program. In addition, you might find that viewing or editing a schema would be helpful. With Visual C# .NET, you can perform the following tasks:
Create and edit raw XML data.
View and edit a schema.
Add XML data.
You can add an XML file to an existing Visual C# .NET project. To do so, choose Add To Project from the File menu. You can then select XML File from the presented options. You also have options to add an XML schema, an XSL transformation (XSLT) file, or a schema file. XSLT is a language for transforming XML documents into other XML documents. XSLT is designed for use as part of XSL, which stands for XML style sheet language.
When you add an XML File template, a new file is added to your project with the following text contained in it:
<?xml version="1.0" encoding="utf-8" ?>
You’re then free to add whatever XML markup you desire. The editor is XML-aware, so it will not only color code, but also provide end tags automatically.
Figure 19-6 shows the Videos.xml file displayed in the Visual C# .NET XML editor.
You can use Visual C# .NET to automatically create a schema for an XML file, or you can create an XML schema from scratch. To create a schema from scratch, you can either right-click an XML file (such as the one shown in Figure 19-6), or you can select XML from the menu. Either technique will display a menu of options, including the Create Schema option. Selecting Create Schema will automatically add a schema file to your project and include the schema for your XML file. Figure 19-7 shows the schema for the Videos.xml file.
Once you have a schema, you have the option to validate your XML data file. If your schema was automatically generated, your XML file will be valid; however, if you created or changed your schema using the schema editor, you should validate your XML data against it. This can be done within the XML editor. Choose Validate XML Data from the XML menu; any problems will be reported in the task list.
Figure 19-7 presented the schema in DataSet view. You can also view and edit the schema in an XML view. To switch views from within the schema editor, right-click the schema editor and choose View XML Source from the shortcut menu. You can also select the XML tab at the bottom of the schema editor window. Figure 19-8 shows the video schema in XML Source view.
If you need to add data to your XML file, you can use one other feature of the Visual C# .NET editor. From within the XML editor, you can select the View Data option by right-clicking the XML editor window or by clicking the editor’s Data tab. A data table will appear, as shown in Figure 19-9, allowing you to enter data directly into your XML file.