Built-In XML Editor

only for RuBoard

In this section, you look at the features provided by the built-in XML editor with a few examples. To do this, it's necessary to start by creating a new ASP.NET web project. (See Figure 4.1.)

Figure 4.1. Creating a C# ASP.NET web project.
graphics/04fig01.gif

This action creates a new virtual directory with the name CustomersXML .Now you can add a new XML file to this project by selecting the Add New item option from the project menu and then choosing an XML file, as shown in Figure 4.2.

Figure 4.2. Adding an XML file to the project.
graphics/04fig02.gif

You can change your editor options by going to Tools, Options, as shown in Figure 4.3.

Figure 4.3. Changing your options in the text editor.
graphics/04fig03.gif

Schema, Data, and XML: The Three Views in XML Designer

The XML Designer provides features that enable you to create and edit XML Schema files, ADO.NET DataSet s, XML data files, and XML source code. These features are provided in the form of the three views: Schema, Data, and XML. This section looks at each of these views individually.

The Data View

The Data view provides a datagrid that can modify .xml files. It enables you to edit the content in an XML file.

The Data Tables and Data separate this view into two areas. The Data Tables area lists relations defined in the XML file in the order of its nesting (from the outermost to the innermost). The Data area is a datagrid that displays data based on the selection in the Data Tables area.

By adding the following code to the newly created XML file and switching to Data view, you can see the datagrid that's shown in Figure 4.4:

Figure 4.4. The Data view.
graphics/04fig04.gif
 <?xml version="1.0" encoding="utf-8" ?>  <Customers xmlns="http://tempuri.org/Customers.xsd">       <Customer id="ALFKI">            <CompanyName>Alfreds Futterkiste</CompanyName>            <Contact>                 <FirstName>Maria</FirstName>                 <LastName>Anders</LastName>                 <Title>Sales Representative</Title>            </Contact>            <Em_u105 ?l>Mario@alfreds-futterkiste.com</Em_u105 ?l>            <Phone>(703) 752-76576</Phone>            <ShippingAddress>                 <Street>City Center Plaza 516 Main St.</Street>                 <City>Elgin</City>                 <St_u116 ?e>OR</St_u116 ?e>                 <Zip>97827</Zip>            </ShippingAddress>            <BillingAddress>                 <Street>12 Orchestra Terrace</Street>                 <City>Walla Walla</City>                 <St_u116 ?e>WA</St_u116 ?e>                 <Zip>99362</Zip>            </BillingAddress>       </Customer>  </Customers> 

To display the Data view correctly, it's essential that the XML be well formed. The Data view generates an error message for a non-well- formed XML document. Newly created XML files contain no data and, therefore, cannot be displayed in Data view. In some cases, although the XML would be considered well formed, if it contains a structure that cannot be displayed by the Data view, trying to switch to Data view generates this message: "Although this document is well formed, it contains structure that Data view cannot display." The following XML code with the node name a repeating at different levels within the hierarchy of the data is an example:

 <abc>       <a><b><a>Hello</a></b></a>  </abc> 
The XML View

The XML view provides an editor for manually editing the XML files and provides color -coding and IntelliSense, including Complete Word and List Members. The editor aligns all the elements when you save the file, so you are relieved of the pain of aligning the XML elements in the file. The file types that can be opened in this view for editing are .xml , .xsd , .xslt , .wsdl , .web , .resx , .tdl , .wsf , .hta , .disco , . vsdisco , and .config . The IntelliSense feature assists you to write well-formed and valid XML. It is available when you work with .xsd files and .xml files that have an associated schema.

To assign a schema to the XML file, first load an XML file into the XML designer by double-clicking the filename in the Solution Explorer. Then change to XML view by clicking the XML tab on the bottom of the designer. Now you can set the targetSchema property to the schema that's going to be associated with the XML document, as shown in Figure 4.5. The Properties window can be invoked by clicking the Properties button in the toolbar. You can also remove the reference to the schema to remove the association between the XML document and the schema.

Figure 4.5. Setting the targetSchema property.
graphics/04fig05.gif

To check if the document is well formed, you can switch from XML view to Data view. If the XML file isn't well formed, the designer shows an error message that indicates the reason and the location where the document is not well formed. Another way to see if a document is well formed is to use the validation feature provided by the IDE, which apart from the validation errors, also displays errors resulting from a non-well-formed document.

Validating the XML File

To validate the XML file against the associated schema, select Validate XML Data from the XML menu. Make sure that the XML file is loaded into the Designer and it's in XML view, as shown in Figure 4.6.

Figure 4.6. Selecting the Validate XML Data menu option.
graphics/04fig06.gif

If no errors are found in the XML file, the status bar indicates this fact with a message similar to "No validation errors were found." If errors are encountered , the Task List gives a description of the errors with their location in the file. Figure 4.7 shows an example of an error generated when the element name BillingAddress was intentionally changed to BillingAddress1 .

Figure 4.7. An error display in the Task List.
graphics/04fig07.gif

As you keep editing the XML file, a thread running in the background keeps validating the file. So, errors are indicated in real-time by wavy lines that underline the elements with the error(s). You can read the descriptions by hovering the mouse over the wavy line. These errors are also reflected in the Task List. (See Figure 4.8.)

Figure 4.8. Viewing the error display in Code Editor.
graphics/04fig08.gif
IntelliSense

While you are manually editing an XML file, you, like everyone else, are prone to make mistakes. By associating an XML Schema to your XML file, Statement Completion is enabled in the XML editor. You can Type < to initiate a tag, and IntelliSense presents you with a list of elements that are valid at that location. (See Figure 4.9.)

Figure 4.9. IntelliSense in the XML editor.
graphics/04fig09.gif

To view a list of attributes that are supported by the element, type the element name and press the spacebar.

The Schema View

The Schema view provides a visual representation of the elements, attributes, types, and so on that make up XML Schemas and ADO.NET DataSets . Schema view allows you to visually construct schemas and ADO.NET DataSets by dropping elements on the design surface from either the XML Schema tab of the toolbox or from Server Explorer. You can create and edit relationship s between tables and edit the keys. You can generate ADO.NET DataSets from XML Schemas.

The XML Schemas in the Visual Studio .NET conform to the specification for schemas published by the World Wide Web Consortium (W3C). The next section looks into the creation of schemas and DataSets .

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net