Using MSXML in .NET


What if you have a ton of code developed using the latest Microsoft parser (currently MSXML 4.0)? Do you have to toss it away and start over if you want to use it with .NET? 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. When you add a reference to msxml4.DLL to your solution, you can start writing some code.

The following examples use books.xml as the source of data. You can download this file from the Wrox Web site (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. As with the other chapters, you can download all code examples in this chapter from the Wrox Web site.

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 list box with the ISBNs from books.xml. You’ll find the full code in the MSXML_Sample folder of the download. You can copy this code into the Visual Studio IDE or create a new Windows Form from scratch. This form contains a list box and a button that use the default names of listBox1 and button1, with the Text property of button1 set to Load XML.

One thing that should be pointed out is that because MSXML 4 is a COM-based component you 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 in Solution Explorer. Why is it MSXML2? When you import a COM component, the namespace that 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 want to.

Next, you take a closer look at the most important lines from the MSXML_sample example code.

Because you now have the reference, you add the line:

  using MSXML2; 

You also need a class-level variable:

  private DOMDocument40 doc; 

Now, you are ready to use MSXML in your application.

You want to take the ISBN from the list box 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 message box. XML Path Language (XPath) is an XML notation that can be used for querying and filtering text in an XML document. You 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 list box:

  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); } 

Next is the event handler for clicking the button. First, you 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 you 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 ();   } } 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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