Liquid XML Data Binder (C++, Java, VB6) / Help With Your Evaluation / Simple Object Viewer
In This Topic
    Simple Object Viewer
    In This Topic

    Background

    The XML Data binding Wizard, generates a set of classes to wrapper an XML schema. Once compiled these classes can be used to load and manipulate XML documents that comply to the schema.

    Overview

    The simple object viewer makes it possible to examine the structure of the generated classes graphically. By selecting an object, and invoking the viewer, a tree of properties is displayed for the object (just like the watch window in Visual Studio and many other IDE’s).

    In essence the viewer allows you to do most of the things you can go programmatically, graphically. This speeds up learning and familiarization and allows instant prototyping – I need to make it output XML that looks a certain way, rather than experimenting in code, you can just experiment with the viewer, then cut and paste the code out.

    Although the viewer works on C# objects, the structure of the VB, C++ & Java class libraries are very similar, so it is still useful even if your not coding C#.

    Features

    • Displays the object hierarchy, including the values of all the properties.
    • Displays the XML for the selected object in the tree.
    • Displays the source code required to re-create the object in its current state.
    • Allows editing of the object tree, changing values, adding to collections etc.
    • Shows validation errors, if the XML generated is not valid, then the viewer indicates the problem.
    • Shows the exceptions raised when invalid properties are read, or set.

    In Detail

    Getting started

    In order to use the viewer you must first have generated a set of classes from an XML schema, see the tutorial on this if you have not done this before.

    You then have 2 options, you can either create an XML document from scratch or load in an existing one.

    For the following walk through will use the XML Schema Bookstore.xsd, so it’s a good idea to have a quick look at it before we start.

    And again, a graphical representation.

    And finally in English!

    The element ‘bookstore’ contains 0-many ‘book’ elements.
           The element ‘book’ contains:-
                  An attribute price which is a decimal
                  An optional attribute ‘publicationdate’ which is a date.
                  An optional attribute ‘ISBN’ which is a string
                  An element ‘title’ which is a string.
                  An optional element ‘genre’ which is a string.
                  An element ‘author’
                  The element ‘author’ contains :-
                         An element ‘first-name’ which is a string.
                         An element ‘last-name’ which is a string.

    Viewing an existing XML document

    Opening up an existing XML document is the simplest way to get to grips with the object tree. We will start by opening the sample file BookStoreSample.xml.

    As you can see the root element in this document is ‘bookstore’. Because of this we need to load the file into the ‘ BookStoreLib.Bookstore’ class, as follows

    BookStoreLib.Bookstore bs = new BookStoreLib.Bookstore();
    bs.FromXmlFile("BookStoreSample.xml");

    The ‘bs’ object is displayed using the the viewer, like this.

    SimpleViewer sv = new SimpleViewer(bs);
    sv.ShowDialog();

    Now you are looking at the objects that hold a representation of the data in BookStoreSample.xml. The ‘Root Node’ is the ‘bs’ object.

    • The tree on the left gives the property names as they appear in the classes.
    • The column on the right gives the value of that property
    • The middle column describes the type of the property as it is declared in the class (first part) and the type of the actual object held in the property (second part), they may differ, as we will see later on.

    As you can see,’bs’ has a property ‘Book’ which is a collection of Book objects (‘BookTypeCol’), and a namespace (the namespace is read only string, and we will cover it more later on). The first (and only) item in the Book Collection, is an Autobiography of Benjamin Franklin, as you can see Author is represented as another class within the book object.

    As you select different parts of the tree, in the bottom panel you can see the XML that is created by each object. By selecting the ‘Code’ tab, you can also see the source code required to re-construct the object. This is very useful when prototyping, and often a pretty good starting point for your development.

    Summary

    We created an instance of ‘ BookStoreLib.Bookstore’, ‘bs’ and loaded the sample file BookStoreSample.xml into it. We then made the viewer to display it.

    Using the viewer we can see all the objects used to represent the XML. We can see the values of individual parts of it. As the graphical representation is an exact representation of the actual objects (it’s created via reflection), we can use it to learn how to navigate around the object model generated from your schema. On top of that we can even obtain the code required to re-produce all/part of the tree. You can also modify the tree, but we will cover that in the next section.

    Creating a document from Scratch

    If you choose to create your document from scratch, you need to select the document element, this is the top level element in the document i.e.

    If we want a document that looks like this

    <?xml version="1.0"?>
    <bookstore>
        <…>
    </bookstore>

    Then our document element is bookstore, and accordingly we need to use the ‘Bookstore’ class.

    BookStoreLib.Bookstore bs = new BookStoreLib.Bookstore();

    Once we have created the bookstore object, we can explore it using the viewer

    SimpleViewer sv = new SimpleViewer(bs); sv.ShowDialog();

    Obviously there is not much to see yet, but just keep in mind that you can see the code needed to populate the object model at any time by selecting the Code tab.

    So we have a pretty sparse book shop at the moment, so lets add some books.

    We can do this by right clicking on the collection of books, and inserting an item.

    Now things are looking a little more interesting, the book collection now has 1 item, if we open that up, we can see that we have a number of properties, some in orange.
    The orange properties indicate that an exception was raised when we tried to read the properties value, and the exception text appears where its value would normally.

    If we take a look at the properties that are orange we notice that they are all the optional ones defined in the schema. Because when the Book object was created (and added to the collection), it did not create any of the optional properties. Because the don’t exist we can’t read them, so when we try we get an exception raised, you can tell that they are invalid without causing an exception to be raised by reading the IsValidXXXX field which tells you if the corresponding field contains any data.

    We can also look at the XML that would be created for the new book (by selecting the book->item[0] node).

    Pretty bland so far, so lets add some data, we can do this by right clicking on a property and selecting ‘Edit Data’, lets start at the top with the Price. We will set it to 6.99, but as you will see the text box will turn red if you enter an invalid value (the tool tip will describe the error)

    The change should is now be reflected in the tree and the XML.

    We can also give a value to the optional properties (the ones in orange), lets give the property ISBN the value ‘1-342154646-45’. You will have to deselect the ‘Make Null’ check box before you can enter your data.

    As you can see, the ISBN field is no longer throwing exceptions when you try to read it, and the IsValidISBN field now indicates that the ISBN field is valid.

    You can also see that an extra field is reflected in the XML

    If we decide later that we don’t want an ISBN attribute associated with this book, we can remove it by setting IsValidISBN to false.