Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Sharpening Your Axis with Visual Basic 9 : Page 2

Visual Basic 9 completely eliminates the barrier between the code you write and the XML you're trying to express. Creating, querying, and transforming XML is much more intuitive and productive than ever before.


advertisement
XML Axis Properties
You use XML axis properties to navigate the XML. There are three axis properties; the descendant axis, the child axis, and the attribute axis. You use the descendant axis property to access all XML elements that have a specified name that are contained in a specified XML element, regardless of the level. You use the child axis property to access just the XML child elements in a given XML element. The child axis performs much better than the descendant axis, and you should use the child axis unless you really need to search for the element name in all levels of the tree. Finally, use the attribute axis property to access the attributes in an XML element.

For example, taking the files example above, you can access all the <name> elements using the descendant axis, which uses a three-dot notation, by simply writing myxml...<name>. If you wanted to transform the above XML, selecting just the name elements to create a new XElement, you would write:

Dim names = <names> <%= myXml...<name> %> </names> names.Save("Names.xml")

That would produce this transformed XML:

<names> <name>customers.html</name> <name>orders.xml</name> <name>plants.xml</name> </names>

You could produce the same simple transformation using child axis properties instead, using a single-dot notation, walking down the hierarchy as shown below:



Dim names = <names> <%= myXml.<files>.<file>.<name> %> </names>

To return the contents of the XElement instead of the object itself you can use the .Value property. For example:

Dim names = _ <names> <fileName> <%= myXml...<name>.Value %> </fileName> </names>

This results in this transformed XML:

<names> <fileName>customers.html</fileName> <fileName>orders.xml</fileName> <fileName>plants.xml</fileName> </names>

To access the isReadonly attribute in the example you'd use the attribute axis property, which uses an ampersand (@) notation. Suppose you want to transform this document and select only the <file> elements where isReadonly is false:

Dim files = <files> <%= From File In myXML...<file> _ Where File.@isReadonly = "false" _ Select File %> </files>

This would produce the following result:

<files> <file isReadonly="false"> <name>orders.xml</name> <created> 2007-10-07T15:57:00.2773392-08:00 </created> <updated> 2007-10-27T12:01:00.3549584-07:00 </updated> <folder>C:\temp</folder> </file> <file isReadonly="false"> <name>plants.xml</name> <created> 2007-10-07T15:57:00.147152-08:00 </created> <updated> 2007-10-18T18:25:07.2607664-07:00 </updated> <folder>C:\temp</folder> </file> </files>

XML axis properties make it much easier to navigate XML, and also improve code readability. One thing to keep in mind when using axis properties is that unlike Visual Basic, XML is case-sensitive. Therefore the case of the axis property does matter. If you had accidentally written myXml...<Name> in the example instead, then it would have returned no elements, because there is no <Name> element in the XML, only lowercase <name> elements. This is definitely something to watch out for but Visual Basic can help. To decrease the likelihood of making mistakes like this you can enable XML IntelliSense.

Enabling XML IntelliSense
Visual Basic can enable IntelliSense for XML elements and attributes defined in an XML schema (XSD). If you include a schema file in your project, the editor will add the applicable elements and attributes to the IntelliSense list of XML properties. To make it easier to infer and add XML schemas to your Visual Basic project, you can download the "XML to Schema" tool from the Visual Basic Developer Center (in the Downloads section).

The XML to Schema tool adds a New Item project template to the list of installed templates that you can use. When you add this item to your project, the XML to Schema wizard opens. The wizard can infer the XML schema set for all the XML sources used in your program, and it will automatically add the schema files to your project. You can specify the XML sources using file paths, URLs that return XML, or by pasting or typing an XML document directly into the wizard. The walkthrough provided on the Visual Basic Developer Center download page for the XML to Schema tool provides detailed instructions.

As an example, suppose you have an XML document that contains customer information. Using the XML to Schema tool you can infer the schema and add the resulting XSD file to your project in one step (see Figure 1). Now, when you access parts of the customers XML using XML axis properties you see IntelliSense based on the schema that was inferred from the XML (see Figure 2).

 
Figure 1. XML to Schema Tool: The XML to Schema tool can infer XML schemas from your input XML documents.
 
Figure 2. XML IntelliSense: After including the appropriate schemas in your project, Visual Basic displays XML IntelliSense.

Confidence Levels for XML IntelliSense
The first time you use XML properties on an XDocument or XElement object variable, the IntelliSense list will contain all the possible elements that can appear based on the information known to the IntelliSense engine through the schema. The icon near the entries in the IntelliSense list will have a red question mark to indicate that the exact XSD type is not known. After you select an item from the IntelliSense list, the next XML property you access in the context of that variable will be displayed in the IntelliSense list as an exact match based on the schema, indicated by a green check mark.

For example, Figure 2 demonstrates IntelliSense items displayed with a green checkmark, because it is clear that the elements listed are the only possible choices for child elements of the <customer> element.

In other words, when IntelliSense can identify a specific type from the schema, it will display possible child elements, attributes, or descendants with a high degree of confidence. However, in cases where IntelliSense is not able to identify a specific type from the schema, it will display an expanded list of possible elements and attributes, but with a low degree of confidence, indicated by a red question mark.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap