Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

830 Project 9 XML WITH VC++.NET

<CustomerNo/>

</TechSupport>

</NewDataSet>

The <TechSupport> nodes get repeated for each customer query. The next step is to parse the XML file and update the ListView control. For this you will use the XmlTextReader class previously described.

 

 

Y

 

Load the XML file into the XmlTextReader object:

 

XmlTextReader *reader= new XmlTextReader(“TechSupp.xml”);

 

 

F

 

Start parsing the document node by node:

 

 

try{

AM

 

 

L

 

while(reader->Read())

 

 

 

{

 

 

 

You

E

 

and

the other

 

are

listed

 

 

1. Check if aTnode is of type Element.

 

 

2. If it is, check if it is of type ID. This is checked because whenever an ID node is encountered, a new row has to be added in the ListView. The Name property of the current node in the XmlReader object will give you the name of the node.

3.If it is a Text type node, add the contents of the Value property to the ListView.

4.If it is of type ReqDate, then shorten the string returned so that the time portion is edited out using the String::Substring method.

if(reader->NodeType==XmlNodeType::Element)

{

if(String::Compare(reader->Name,”ID”)==0)

{

reader->Read();

//new row In the ListView lvi=lv->Items->Add(reader->Value);

if(String::Compare(reader->Name,”ReqDate”)==0)

Team-Fly®

XslTrans-

XML WITH VC++.NET Chapter 28 831

{

reader->Read();

//The first 10 characters give you the date, so discard the rest. lvi->SubItems->Add((reader->Value)->Substring(0,10)) ;

}

}

else

{

if(reader->NodeType==XmlNodeType::Text)

{

lvi->SubItems->Add(reader->Value);

}

}

}

}

catch(Exception *e)

{

Console::WriteLine(e->Message);

}

reader->Close();

}

Now the data in the XML file can be parsed and displayed in the ListView. All that is left now is to add the functionality for the event handler for the button labeled Generate Report.

The method will make use of another fundamental .NET XML class, XslTransform, found in the System::XML::Xsl namespace. This class is used to transform an XML document using an XSL stylesheet.

The steps involved are simple. First create an object of the type XslTransform, and then load the XSL file using the XslTransform::Load method, which accepts the URL of an XSL file as a parameter. Transform it by calling the form::Transform method. This method accepts two parameters, the name of the XML file to be transformed and the name of the file to store the transformation in.

832 Project 9 XML WITH VC++.NET

void MyForm::ConvHtm_Click(Object *sender, System::EventArgs *e)

{

XslTransform *xTrans=new XslTransform(); xTrans->Load(“HTM.xsl”);

try

{

xTrans->Transform(“TechSupp.xml”,”TechSupp.html”);

}

catch(System::Xml::XmlException *ex)

{

MessageBox::Show(ex->Message,” “);

}

}

Next, take a look at the XSL file that is used for converting the XML file into an HTML file.

This is a very simple XSL file. Just structure the XML elements into a table and add the required HTML tags.

<?xml version=”1.0”?>

<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”NewDataSet”>

<html>

<body>

<h3>Daily Report</h3>

<table border=”1”>

<tr>

<td>Date</td>

<td>Location</td> <td>Category </td> <td>Detail </td> <td>Customer Number </td> </tr>

<xsl:for-each select=”/NewDataSet/TechSupport”>

XML WITH VC++.NET Chapter 28 833

<tr>

<td><xsl:value-of select=”ReqDate”/> </td>

<td><xsl:value-of select=”ReqLocation”/> </td>

<td><xsl:value-of select=”Category”/> </td>

<td><xsl:value-of select=”Detail”/> </td>

<td><xsl:value-of select=”CustomerNo”/> </td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Build the application and copy the HTM.xsl file, created in the preceding section, into the folder containing the application’s executable. Execute the application. First enter the name of the technical support person as it appears in the database and click on the Get Data button. Once the XML file has been created and the ListView is populated with the data, the Generate Report button should become enabled. Click on the Generate Report button to generate an HTML file. Open the generated HTML file and verify its contents with the contents of the database.

Summary

In this chapter, you created a Managed C++ application that uses the .NET classes in the Xml namespace to create and manipulate XML documents and data. You also learned how the .NET Framework’s XML classes simplify XML access and manipulation. While providing faster, lighter classes for XML reading and writing, the .NET Framework also provides classes that allow you to access XML documents using the traditional DOM API.

This page intentionally left blank

PARTXIProfessional

Project – 10

This page intentionally left blank

Project 10

Networking and

Remoting in

VC++.NET

Project 10 Overview

Networking applications on the Windows platform can be built as protocolspecific applications that use sockets for protocols, such as TCP and UDP, or applications that use platform-specific protocols or technologies, like DCOM, for communication. Building remote applications using DCOM is easier than doing it with sockets. However, developing applications using sockets is more flexible and can be adapted to other platforms also.

DCOM is widely used to create client/server and n-tier applications on the Windows platform. However, in the .NET platform, it has been replaced by the .NET Remoting technology. .NET Remoting provides a way for applications in different machines/domains to communicate with each other.

This section has two projects: one covers the basics of networking and the other caters to remoting.

The first project that illustrates networking guides you in creating a chat application using socket programming. The second project that illustrates remoting guides you in creating a Quiz application.

The concepts that you will use in these two projects include the following:

Sockets

.NET Remoting

Chapter 29

Implementing

Networking