Export (0) Print
Expand All

Upgrading Components That Use the com.ms.xml Package

Visual Studio 2005

The classes in the com.ms.xml.* packages can be used to load, read, modify, and save XML documents. The documentation in Visual J++ 6.0 and the Microsoft SDK for Java discuss these classes in detail. Visual J# does not support these packages. While upgrading a Visual J++ application that uses these packages, it is recommended that you move to the corresponding classes in the .NET Framework System.Xml namespace.

The classes in System.Xml provide functionality equivalent to the com.ms.xml.* packages and are conformant with W3C standards. In the com.ms.xml.* packages, the Document, ElementCollection, ElementImpl, and Element classes are among the most used. The XMLDocument, XMLNodeList, and XMLNode classes in System.Xml are direct equivalents.

The following example shows how a Visual J++ application that reads a simple XML file can be upgraded to Visual J# using the classes in System.Xml.

SampleFile.Xml

<?xml version="1.0" encoding="utf-8" ?> 
<bookdetails>
    <publisher>Microsoft Press</publisher>
    <book>
          <name>Designing Component-Based Applications</name>
          <price>12.45</price>
    </book>
    <book>
          <name>Professional Visual C++ MTS Programming</name>
          <price>21.90</price>
    </book>
    <book>
          <name>Essential COM+, 2nd Edition</name>
          <price>33.10</price>
    </book>
</bookdetails>

Visual J++ 6.0 Application

import com.ms.xml.om.*;
public class XMLUpgrade
{
   public static void main(String[] args) 
   {
      try 
      {
         // Create an XML document object and load an XML file
         Document xmlDoc = new Document();
         xmlDoc.load("SampleFile.xml");

         // Get the root node of the document
         Element xmlElement = xmlDoc.getRoot();
         // Get a collection of child nodes under the root node
         ElementCollection xmlEleCo1 = xmlElement.getChildren();
         // Get all nodes named 'book' in the collection
         ElementCollection xmlEleCo2 = (ElementCollection) 
         xmlEleCo1.item("book");
         int length = xmlEleCo2.getLength();
         for (int i=0; i < length; i++) 
         {
            // Retrieve the current element (book) from the collection
            Element xmlElement1 = xmlEleCo2.getChild(i);
            // Print the non-marked up text of the first child 
            // (book name)
            System.out.println(xmlElement1.getChild(1).getText());
         }
      }
      catch(Exception e) 
      {
         System.out.println("An exception occurred");
      }
   }
}

After Upgrading to Visual J#

import System.Xml.*;
public class XMLUpgrade 
{
   public static void main(String [] args)  
   {
      try 
      {
         // Create an XML document object and load an XML file
         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.Load("SampleFile.xml ");

         // Get all nodes named 'book' in the document
         XmlNodeList xnl = xd.GetElementsByTagName("book");
         int length = xnl.get_Count();

         XmlNode xn = null;
         for (int i=0; i < length; i++)
         {
            // Retrieve the current element (book) from the collection
            xn = xnl.Item(i);

            // Print the non-marked-up text of the first child 
            // (book name)
            System.Console.WriteLine(xn.get_FirstChild().get_InnerText());
         }
      }
      catch (Exception e) 
      {
         System.Console.WriteLine("An exception occurred");
      }
   }
}
NoteNote

The changes to the source code need to be made manually. The Upgrade Wizard will not automatically add the .NET Framework classes to the application.

Community Additions

ADD
Show:
© 2014 Microsoft