Using MSXML in .NET

 
Chapter 11 - Manipulating XML
bySimon Robinsonet al.
Wrox Press 2002
  

What if you have a ton of code developed using Microsoft's latest parser (currently MSXML 4.0)? Do you have to toss it away and start over if you want to use it with .NET? On the other hand, what if you are comfortable using the MSXML 4.0 DOM? Do you have to switch to .NET right away?

The answer is "no". XML 4.0, 3.0 or 2.0 can be used directly in your applications. Once you add a reference to msxml4. DLL to your solution, you can start writing some code.

The next few examples will use books.xml as the source of data. This file can be downloaded from the Wrox web site ( http://www.wrox.com/ ), but it is also included in several examples in the .NET SDK. The books.xml file is a book catalog for an imaginary bookstore. It includes book information such as genre , author name , price, and ISBN number. All of the code examples in this chapter are also available on the Wrox web site. In order to run the examples, the XML data files will need to be in a path structure that looks something like this:

 /XMLChapter/Sample1 /XMLChapter/Sample2 /XMLChapter/Sample3 ... 

Actually, you can call the directories anything you wish, but the relative position is important. You can also modify the examples to point to anywhere you wish. The example code will be commented to show which line(s) to change if you wish to do this.

This is what the books.xml file looks like:

   <?xml version='1.0'?>     <!-- This file represents a fragment of a book store inventory database -->     <bookstore>     <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">     <title>The Autobiography of Benjamin Franklin</title>     <author>     <first-name>Benjamin</first-name>     <last-name>Franklin</last-name>     </author>     <price>8.99</price>     </book>     <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">     <title>The Confidence Man</title>     <author>     <first-name>Herman</first-name>     <last-name>Melville</last-name>     </author>     <price>11.99</price>     </book>     <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">     <title>The Gorgias</title>     <author>     <name>Plato</name>     </author>     <price>9.99</price>     </book>     </bookstore>   

Let's look at some code that uses MSXML 4.0 to load a listbox with the ISBNs from books.xml . You'll find the full code in the MSXML_Sample folder of the download. You can copy this into the Visual Studio IDE or create a new Windows Form from scratch. This form contains a listbox and a button, as you can see opposite . Both use the default names of listBox1 and button1 , with the Text property of button1 set to Load XML .

click to expand

One thing that should be pointed out is that since MSXML 4 is a COM-based component, we will need to create the interop assembly. The easiest way is to select Add Reference from the Project menu in the Visual Studio IDE. Go to the COM tab and select Microsoft XML, v4.0 (or v3.0 , v2.6 ). You will see MSXML2 as the added namespace Solution Explorer . Why is it MSXML2 ? When you import a COM component the namespace is given to the new assembly is the typelib name for the COM component. In this case it is MSXML2 . If you use TLBIMP you can change the namespace to something else if you wish to.

Let's now have a look at the most important lines from the MSXML_sample example code.

Since we now have the reference, we add the line:

   using MSXML2;   

We also need a class-level variable.

   private DOMDocument40 doc;   

Now we are ready to use MSXML in our application.

We want to take the ISBN from the listbox, and, using a simple XPath search, find the book node that it matches and display the node text (the book title and book price) in a MessageBox . XML Path Language (XPath) is an XML notation that can be used for querying and filtering text in an XML document. We will look more closely at how to use XPath in .NET later in the chapter.

Here is the event handler code for selecting an entry in the listbox:

   protected void listBox1_SelectedIndexChanged (     object sender, System.EventArgs e)     {     string srch=listBox1.SelectedItem.ToString();     IXMLDOMNode nd=doc.selectSingleNode(     "bookstore/book[@ISBN='" + srch + "']");     MessageBox.Show(nd.text);     }   

Now we'll look at the event handler for clicking the button. First, we load the books.xml file - note that if you're running the executable from somewhere that isn't the bin/debug or bin/release folder, you'll need to adjust the path appropriately:

   protected void button1_Click (object sender, System.EventArgs e)     {     doc=new DOMDocument40 ();     doc.load("..\..\..\books.xml");   

The next lines declare that nodes is a NodeList of book nodes. In this case there are three book nodes:

   IXMLDOMNodeList nodes;     nodes = doc.selectNodes("bookstore/book");     IXMLDOMNode node=nodes.nextNode();   

Then we loop through the nodes, and add the text value of the ISBN attribute to listBox1 :

   while(node!=null)     {     listBox1.Items.Add(node.attributes.getNamedItem("ISBN").text);     node=nodes.nextNode ();     }     }   

The left-hand screenshot below shows the sample executing. After clicking the button, the listbox is loaded with the book ISBNs. The output after selecting an ISBN is shown in the right-hand screenshot below.

click to expand
  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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