Advanced XML Web Programming - Dalat College of Technology - 31

"nsHello", // Assembly

"nsHello.Hello", // Full type name

"host/Hello.soap", // URI WellKnownObjectMode.Singleton ); // Object Mode

/* Wait until the user wants to exit */

Console.WriteLine( "Listening for requests - Press ENTER to exit" ); String keyState = Console.ReadLine();

}

}

In the .NET Framework messages are passed back and forth from the handler object through Channels Objects that encapsulate the underlying network protocols that make up the client/server connection. In hello_host.cs, we want to use the built-in HTTP Channel, which transports messages using the SOAP protocol.

makefile: A compile file for hello_host.cs and hello.cs. all: hello_host.exe hello.dll

hello_host.exe: hello_host.cs

csc /r:System.Runtime.Remoting.dll hello_host.cs hello.dll: hello.cs

csc /t:library hello.cs Client

Now that the server is created we can focus on the client. The hello_client.cs file is an application that will consume the service exposed by our hello_host.cs to invoke the remote method SayHello.

hello_client.cs: A consumer of hello_host.cs using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels.HTTP; using nsHello;

public class HelloClient

{

public static Main( String[] args )

{

/* Obtain a Proxy to the SOAP URL */ Hello h = (Hello)Activator.GetObject(

typeof(Hello), "http://localhost:1099/host/Hello.soap" );

/* The following occurs over SOAP to HelloHost */ Console.WriteLine( h.SayHello( "Dick" ) );

}

}

In hello_client.cs we use the GetObject method instead of the new keyword to instantiate the remote object because we are manually instantiating the object. Had we not used the remote configuration profile, we could have used the New keyword to the same effect.

With the previous example, we need to compile the application. The following command will compile the hello_client.cs file:

csc /r:System.Runtime.Remoting.dll /r: hello.dll hello_client.cs

Once both the server and client applications are built, we can test them by first running hello_host.exe in one window and then running hello_client.exe in the other window. You should see hello_client.exe respond with a “Hello Dick” message on the screen.

II. Install e-Business applications using Biztalk Server

Unless we're working for a pure dot-com enterprise, we're probably allowing data to reside in some older database, inside flat files, or some other type of resource. If this is the case, and you still have a need to access, update, and use this data, you've probably found yourself

face the performance and flexibility flowing out. Simple systems may not have appropriate methods of accessing and using application data.

Over the next few sections we will discuss the applications of BizTalk and how it can be used to solve these problems. We will talk about enterprise application integration, business-to-business (B2B) integration, and business process automation.

Want to read some real-world case studies of how BizTalk has been used for this different implementation and resolution? Check out http://www.microsoft.com/biztalk/evaluation/casestudies for a list of companies and documentation on their implementations.

Enterprise application integration

In a tech world where many Java, C#, or some other scripting language programmer represent all the legal methods and the oldest systems are Windows 98 or Windows NT 4. Search Results COBOL, nMainframes, a Virtual Memory System (VMS), Virtual Address eXtension (VAX), and text-based displays that serve as the backbone to entire businesses can be commonplace, especially in large corporations. These legacy systems are widely used, deployed, and relied upon for ongoing business operations.

Above all else, the BizTalk application server is designed for ease of integration. It uses heavy industry standards, like XML, SOAP, and the BizTalk Framework. It provides tools that allow us to create XML representations of our legacy data and coordinate access and use of that data. This reduces your integration into the legacy system to simply supporting some basic XML features, and BizTalk takes it from there.

Data inheritance, however, is not the only data that BizTalk can communicate with and integers. In fact, as we create our XML-based web services as the cover of this book, we may have found the BizTalk server to be the perfect system to handle the processing of messages.

Business-to-Business Integration

Integration with internal applications is just one type of useful implementation for BizTalk Server. Many businesses have built XML-based systems or interfaces that allow them to automate their communications and exchanges with customers and partner companies. For example, a retail company might capture product descriptions and pricing in an XML format. Using BizTalk Server is the ability to map one model to another, you can feel confident that you can design your own models that are right for your business, and yet not limit your integration with partners.

The BizTalk Mapper parser allows us to draw lines between the elements and attributes of the two models, resulting in an XSLT stylesheet that will transform our partner's data into a format that is accessible to our system. This capability allows you to build an XML-based solution within your company.

Business process automation

Business process automation is moving to the fore today as economics. If a company can reduce the time and costs associated with a particular task, such as inventory control, the company's profits will probably not increase unless it has reduced its bottom-line costs and such profits are increasing. Automation can be a tremendously important move and cost saver for a company, especially when finances are a major concern.

BizTalk Orchestration Designer, together with Messaging Manager, BizTalk provides a visual dashboard that is a visual approach to designing, developing, and automating business processes. With these tools they can design entire processes to handle input requests or documents and create program records that can be processed and tracked.

III. Data Access and XML

Before Windows.NET, OLEDB and ActiveX Data Objects (ADO) were the primary APIs for accessing data in Microsoft Windows. With the introduction of .NET, Microsoft introduced

a new branch of data access model called ADO.NET, it supports data access model and functionality perfect for multi-tier, distributed web environment.

All three data access object models, OLEDB, ADO, and ADO.NET, provide varying levels of XML support. Let's first examine the three models and their support for XML.

OLEDB and ADO

Both OLEDB and ADO are based on COM technology. OLEDB provides a low-level and highly efficient method of accessing and manipulating relational data, while ADO is built on OLEDB, providing an easier means of accessing data. XML support in ADO is provided through Recordset Persistence and is implemented by the Microsoft OLE DB Persistence Provider. This provider can create an ADO Recordset object, generate an XML document and related schema information, and store everything to a stream or file. Similarly, this provider can also generate a read-only and forward-only Recordset object from an XML file or stream.

The example below shows how we can create a Recordset object to generate an XML file.

ADORecordset2XML.cs: Persisting an ADO Recordset object to an XML file. using System;

using System.Data; using System.Data.SQL;

public class ADORecordset2XML

{

public static void Main( String[] args )

{

try

{

String connString ="provider=SQLOLEDB;" + "initial catalog=pubs;" + "server=localhost;" +

"uid=sa;" + "pwd=";

String queryString ="select * from titles";

/* Instantiate a Recordet object */ ADODB.Recordset rs = new ADODB.Recordset();

/* Connect to the database and execute a SQL query */ rs.Open( queryString, connString,

ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, 0 );

/* Export Recordset to "titles.xml" file in XML format. */

rs.Save( "titles.xml", ADODB.PersistFormatEnum.adPersistXML );

/* close the Recordset object */ rs.Close();

}

catch (Exception e)

{

Console.WriteLine( "Exception: {0}", e.ToString() );

}

}

}

In the above example we created titles.xml file, in the below example we will look at creating Recordset object from the above titles.xml file.

XML2ADORcordset.cs: Recreating the Recordset object from the titles.xml file. using System;

using System.Data; using System.Data.SQL;

public class XML2ADORecordset

{

public static void Main( String[] args )

{

try

{

String connString ="provider=SQLOLEDB;" + "initial catalog=pubs;" + "server=localhost;" +

"uid=sa;" + "pwd=";

Int32 adCmdFile = 256;

/* Instantiate Recordset object */ ADODB.Recordset rs = new ADODB.Recordset();

/* Open the titles.XML file into Recordset object */ rs.Open( "titles.xml", connString,

ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, adCmdFile );

/* Display the value of the price field for the first record */ rs.MoveFirst();

Console.WriteLine( rs.Fields[ "price" ].Value );

/* close the Recordset object */ rs.Close();

}

catch (Exception e)

{

Console.WriteLine( "Exception: {0}", e.ToString() );

}

}

}

XML support in ADO is limited to say the least. You can only still create and rebuild Recordset objects back and forth between the XML file and the stream. The XML file remains separate from the old Recordset object and any changes we make will not be reflected automatically.

ADO.NET

ADO.NET is touted as the successor to ADO and is XML-based. The core focus of ADO.NET will be providing an API that facilitates the creation of distributed, scalable, and interactive applications and the sharing of data in a persistent, disconnected fashion.

ADO.NET consists of two parts: the Dataset class and Managed Providers. The Dataset class provides methods and properties for accessing and manipulating relational data. . Managed Providers represent the underlying data store as a Microsoft SQL Server database.

Namespaces built in ADO.NET

Namespace

Interpretation

System.Data

Contains the Dataset class and other base classes

System.Data.ADO

Contains ADO Managed Provider classes.

System.Data.SQL

Contains SQL Server Managed Provider classes.

System.Data.SQLite

Contains classes for data types in SQL Server.

Maybe you are interested!

Advanced XML Web Programming - Dalat College of Technology - 31

DataSet Class

The DataSet class is central to ADO.NET and is the primary package for data access and manipulation in the .NET Framework. A DataSet is a non-persistent, in-memory representation of a database. You can have a DataSet dynamically created and populated with data without a single byte of communication being made with a DBMS. De-

This coupling between the data and the underlying data source is the core enabling of ADO.NET persistent connection behavior.

Internally, we can think of a DataSet as an XML document containing one or more loose-leaf or related XML elements arranged in a tabular fashion. In fact, .NET provides a unified programming model for accessing data represented as both XML and relational data. You can start with a DataSet and work with the data following a non-sequential, hierarchical path controlled by the XML DOM. In turn, you can work with XML documents in a row-by-row fashion through a DataSet using a relational model.


The XmlDataDocument class

The XmlDataDocument class extends the XmlDocument class itself, which is the primary ADO.NET wrapper for an XML document. XmlDataDocument provides additional capabilities for manipulating relational data. Like all other XML-related classes, XmlDataDocument resides in the System.Xml namespace.

In the following example, use C# code to retrieve data from a SQL Server database and use XmlDataDocument to manipulate the data:

DataSet2XmlDataDocument.cs: Manipulating relational data using the DOM. using System;

using System.IO; using System.Data;

using System.Data.SQL; using System.Xml;

public class DataSet2XmlDataDocument

{

public static void Main( String[] args )

{

SQLConnection conn = null; try. try

{

/* connect to the SQL Server database */

conn = new SQLConnection( "server= localhost;uid=sa;pwd=;database=pubs" );

/* execute SQL query */

SQLDataSetCommand cmd = new SQLDataSetCommand( "select * from Titles",

conn );


/* create a DataSet and populate it with the records returned from result of the above SQL query */

DataSet ds = new DataSet(); cmd.FillDataSet( ds, "Titles" );

/* associate this DataSet with an XmlDataDocument */ XmlDataDocument doc = new XmlDataDocument( ds );

/* you can now process this document just like any

DataDocument. For example,

the following uses DataDocumentNavigator to select the first Titles node and modifies the value. */

DataDocumentNavigator nav = new DataDocumentNavigator

( doc );


nav.Select( "//NewDataSet/Titles[1]/title_id" ); if ( nav.MoveToNextSelected() )

{

Console.WriteLine( "Original=" + nav.InnerText );

}

else

{

Console.WriteLine( "Unexpected error: Node not found" );

}

nav.InnerText ="BU1030"; /* modify the value of the title_id node */

/* XmlDataDocument auto synchronizes the above change with the associated DataSet. */

Console.WriteLine( "Modified=" + ds.Tables[ "Titles" ].Rows[ 0 ][ "title_id" ] );

}

finally

{

if ( conn != null && conn.State == DBObjectState.Open )

{

conn.Close();

}

}

}

}

The following example manipulates data using a Dataset XmlDataDocument2DataSet.cs: Manipulating an XML document using a DataSet. using System;

using System.IO; using System.Data; using System.Xml;

public class Xml2DataSet

{

public static void Main()

{

try

{

/* instantiate an XmlDataDocument */ XmlDataDocument doc = new XmlDataDocument();

/* load the schema */ doc.DataSet.ReadXmlSchema( "book.xsd" );

/* now load the XML document */ doc.Load( "book.xml" );

/* print original price value of first Title node */ DataDocumentNavigator nav = new DataDocumentNavigator( doc ); nav.Select( "//Book/Titles[1]/price" );

if ( nav.MoveToNextSelected() )

{

Console.WriteLine( "Original=" + nav.InnerText );

}

/* update a price using the associated DataSet */ DataTable books = doc.DataSet.Tables[ "Titles" ]; books.Rows[ 0 ][ "price" ] ="12.99";

/* display modified price */ nav.Select( "//Book/Titles[1]/price" ); if ( nav.MoveToNextSelected() )

{

Console.WriteLine( "Modified=" + nav.InnerText );

}

}

catch (Exception e)

{

Console.WriteLine("Exception: {0}", e.ToString());

}

}

}

The following example is the xml document used above:

book.xml: An XML document used in XmlDataDocument2DataSet.cs.

<Book>

<Titles>

<title_id>BU1032</title_id>

<title>The Busy Executive's Database Guide</title>

<type>business</type>

<pub_id>1389</pub_id>

<price>19.99</price>

<advance>5000</advance>

<royalty>10</royalty>

<ytd_sales>4095</ytd_sales>

<notes>An overview of available database systems with emphasis on common business applications. Illustrated.</notes>

<pubdate>1991-06-11T16:00:00</pubdate>

</Titles>

<Titles>

<title_id>BU1111</title_id>

<title>Cooking with Computers: Surreptitious Balance Sheets

</title>


<type>business</type>

<pub_id>1389</pub_id>

<price>11.95</price>

<advance>5000</advance>

<royalty>10</royalty>

<ytd_sales>3876</ytd_sales>

<notes>Helpful hints on how to use your

electronic resources to the best advantage.</notes>

<pubdate>1991-06-08T16:00:00</pubdate>

</Titles>

<Titles>

<title_id>BU2075</title_id>

<title>You Can Combat Computer Stress!</title>

<type>business</type>

<pub_id>0736</pub_id>

<price>2.99</price>

<advance>10125</advance>

<royalty>24</royalty>

<ytd_sales>18722</ytd_sales>

<notes>The latest medical and psychological techniques for living with the electronic office. Easy-to-understand explanations.</notes>

<pubdate>1991-06-29T16:00:00</pubdate>

</Titles>

</Book>

Here is the schema file used for the book.xml file: 16-6 book.xsd: The XML schema for book.xml.

<xsd:schema id="Book" targetNamespace="" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml -msdata">

<xsd:element name="Titles">

<xsd:complexType content="elementOnly">

<xsd:all>

<xsd:element name="title_id" type="xsd:string"/>

<xsd:element name="title" type="xsd:string"/>

<xsd:element name="type" type="xsd:string"/>

<xsd:element name="pub_id" minOccurs="0" type="xsd:string"/>

<xsd:element name="price" minOccurs="0" type="xsd:decimal"/>

<xsd:element name="advance" minOccurs="0" type="xsd:decimal"/>

<xsd:element name="royalty" minOccurs="0" type="xsd:int"/>

<xsd:element name="ytd_sales" minOccurs="0" type="xsd:int"/>

<xsd:element name="notes" minOccurs="0" type="xsd:string"/>

<xsd:element name="pubdate" type="xsd:timeInstant"/>

</xsd:all>

</xsd:complexType>

</xsd:element>

<xsd:element name="NewDataSet" msdata:IsDataSet="True">

<xsd:complexType>

<xsd:choice maxOccurs="unbounded">

<xsd:element ref="Titles"/>

</xsd:choice>

</xsd:complexType>

</xsd:element>

</xsd:schema>

IV. Parsing and generating XML documents.

XmlReader and XmlWriter are abstract classes of the XML object model in

.NET Framework. XmlReader provides an API for reading XML documents, while XmlWriter provides an additional API for producing W3C-compliant XML documents. In designing these classes, Microsoft borrowed concepts from both the DOM and SAX.

XmlReader

The abstract XmlReader class provides a fast, read-only, and forward-looking cursor to XML documents. Using XmlReader is much like using the DOM, you read and work with one node at a time. The following C# method traverses an XML document and displays the names of all the elements in the document.

public void DisplayElements( XmlReader reader )

{

/* read the next node in document order */ while ( reader.Read() )

{

/* if this is an element node, display its name */ Console.WriteLine( reader.Name );

}

}

The following C# method will display the value in the current element: public void DisplayNode( XmlReader node )

{

switch ( node.NodeType )

{

case XmlNodeType.Element: Console.Write( "<" + node.Name + ">" ); break; break;

case XmlNodeType.Text: Console.Write( node.Value ); break; break;

case XmlNodeType.CDATA: Console.Write( node.Value ); break; break;

case XmlNodeType.ProcessingInstruction:

Console.Write( "<?" + node.Name + " " + node.Value + "?>" ); break; break;

case XmlNodeType.Comment: Console.Write( "<!--" + node.Value + "-->" ); break; break;

case XmlNodeType.Document: Console.Write( "<?xml version='1.0'?>" ); break; break;

case XmlNodeType.Whitespace:

Comment


Agree Privacy Policy *