Click here to Skip to main content
15,896,118 members
Articles / Desktop Programming / Win32

Schemaless C#-XML data binding with VTD-XML

Rate me:
Please Sign up or sign in to vote.
4.31/5 (23 votes)
25 Jun 2008CPOL9 min read 74.1K   353   54  
Agile, efficient XML data binding without schema.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using com.ximpleware;

namespace XMLBinder3
{
    class XMLBinder3
    {
        // convert an XML file into a .vxl file here
        void indexDocument(String XMLFileName, String indexName)
        {
            VTDGen vg = new VTDGen();
            if (vg.parseFile(XMLFileName, true))
            {
                vg.writeIndex(indexName);
            }
        }

        ArrayList bind(VTDNav vn)
        {
            ArrayList al = new ArrayList();
            AutoPilot ap0 = new AutoPilot();
            AutoPilot ap1 = new AutoPilot();
            AutoPilot ap2 = new AutoPilot();
            AutoPilot ap3 = new AutoPilot();
            AutoPilot ap4 = new AutoPilot();
            ap0.selectXPath("/CATALOG/CD[YEAR > 1982 and YEAR < 1990]");
            ap1.selectXPath("TITLE");  /*/CATALOG/CD[YEAR > 1982 and YEAR < 1990]/TITLE */
            ap2.selectXPath("ARTIST"); /* /CATALOG/CD[YEAR > 1982 and YEAR < 1990]/ARTIST */
            ap3.selectXPath("PRICE"); /* /CATALOG/CD[YEAR > 1982 and YEAR < 1990]/PRICE */
            ap4.selectXPath("YEAR"); /* /CATALOG/CD[YEAR > 1982 and YEAR < 1990]/YEAR */
            ap0.bind(vn);
            ap1.bind(vn);
            ap2.bind(vn);
            ap3.bind(vn);
            ap4.bind(vn);
            while (ap0.evalXPath() != -1)
            {
                CDRecord2 cdr = new CDRecord2();
                cdr.title = ap1.evalXPathToString();
                cdr.artist = ap2.evalXPathToString();
                vn.push();
                ap3.evalXPath();
                cdr.priceIndex = vn.getText();
                cdr.price = vn.parseDouble(cdr.priceIndex);
                ap3.resetXPath();
                vn.pop();
                cdr.year = (int)ap4.evalXPathToNumber();
                al.Add(cdr);
            }
            ap0.resetXPath();
            return al;
        }

        public static void Main(String[] args)
        {
            XMLBinder3 xb = new XMLBinder3();
            VTDGen vg = new VTDGen();
            XMLModifier xm = new XMLModifier();
            xb.indexDocument("D:\\codeProject\\app1\\XMLBinder3\\XMLBinder3\\catalog.xml",
                "D:\\codeProject\\app1\\XMLBinder3\\XMLBinder3\\catalog.vxl");

            VTDNav vn = vg.loadIndex("D:\\codeProject\\app1\\XMLBinder3\\XMLBinder3\\catalog.vxl");
            ArrayList al2 = xb.bind(vn);
            IEnumerator ie2 = al2.GetEnumerator();
            xm.bind(vn);
            while (ie2.MoveNext())
            {
                CDRecord2 cdr = (CDRecord2)ie2.Current;  // reduce prices by 1
                xm.updateToken(cdr.priceIndex, "" + (cdr.price - 1));
            }
            xm.output("D:\\codeProject\\app1\\XMLBinder3\\XMLBinder3\\cd_update.xml");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer XimpleWare
United States United States
Jimmy Zhang is a cofounder of XimpleWare, a provider of high performance XML processing solutions. He has working experience in the fields of electronic design automation and Voice over IP for a number of Silicon Valley high-tech companies. He holds both a BS and MS from the department of EECS from U.C. Berkeley.

Comments and Discussions