Listing Traversing DOM Tree Using XmlDocument Class
lt Page Language C gt lt Import Namespace System.Xml gt lt script runat server gt void Page_Load object sender, EventArgs e string xmlPath Request.PhysicalApplicationPath App_Data Books.xml XmlDocument doc new XmlDocument doc.Load xmlPath XmlNode rootNode doc.DocumentElement DisplayNodes rootNode Print the node type, node name and node value of the node if node.NodeType XmlNodeType.Text Response.Write Type node.NodeType Value node.Value lt br gt Response.Write Type node.NodeType Name node.Name...
The XmlSerializer Class
The central element in the XML serialization architecture is the XmlSerializer class, which belongs to the System.Xml.Serialization namespace. The XML serialization process can be used to quickly develop an XML-driven application and get the best of both worlds objects for programming and XML for data transfer or storage. The XmlSerializer does the transformations from one representation to the other. To accomplish its functionalities, the XmlSerializer class exposes the methods shown in Table...
Writing Images Using XmlWriter
The techniques described in the previous sections can also be used with any sort of binary data that can be expressed with an array of bytes, including images. This section provides you with an example and demonstrates how to embed a JPEG image in an XML document. The structure of the sample XML document is extremely simple. It consists of a single employee node, and inside that node there is an image node holding the binary image data plus an attribute containing the original file name. Code...
Retrieving the XML Data Type Column from Client
This section focuses on the out-of-process access to the XML data type from a client ASP.NET page. There are different ways to retrieve an XML data typed column using a SqlDataReader object. Using the methods of the SqlDataReader class, you can retrieve the XML data either as a string or as an SqlXml object. The next section starts by exploring the steps involved in retrieving the contents of an XML column as a string. Retrieving the XML Data Type Column as a String Listing 10-4 retrieves the...
WebConfigurationManager Class
One of the important configuration-related classes is the WebConfigurationManager class that provides programmatic access to configuration files and configuration sections. This class is contained in the System.Web.Configuration namespace and is specifically used for retrieving and updating configuration settings from a web.config file. Table 14-3 discusses the properties of the WebConfigurationManager class. Table 14-3. Properties of the WebConfigurationManager Class Table 14-3. Properties of...
Building an Airline Reservation System using ASPNET and SQL Server
This case study ties together all the concepts including XML DOM, XML support in ADO.NET, XSLT features in .NET, XML data display, that have been covered so far in this book. The focus of this case study is on incorporating these XML features in a real world airline reservations Web site and showcasing the best practices of using these XML features. I also discuss the N-Tier design methodology and illustrate how to leverage that to create an extensible and flexible airline reservations system.
Enabling Decompression in a Web Service Client
Now with .NET 2.0, enabling decompression on the client side just requires one line of code. All you need to do is to set the EnableDecompression property on an instance of a client proxy class to true, as follows. CategoriesService obj new CategoriesService obj.EnableDecompression true This gives the Web clients the capability to interoperate with services that support compression. To disable decompression, set EnableDecompression to false. Setting this property to false enables the server to...
Steps Involved in Using XmlReader to Read XML Data
The XmlReader class is designed for fast, forward-only access to the contents of an XML file, and is not suited for making modifications to the file's contents or structure for that you will use the XmlDocument class . The XmlReader class works by starting at the beginning of the file and reading one node at a time. As each node is read, you can either ignore the node or access the node information as dictated by the needs of the application. The steps for using the XmlReader class are as...
The InferXmlSchema Method
You can also use the InferXmlSchema method to load the DataSet schema from an XML document. This method has the same functionality as that of the ReadXml method that uses the XmlReadMode enumeration value set to InferSchema. The InferXmlSchema method, besides enabling you to infer the schema from an XML document, enables you to specify the namespaces to be ignored when inferring the schema. This method takes two parameters. The first parameter is an XML document location, a stream, or an...
Implementation of FileSystemWatcher to Facilitate Reporting Data Collection
Now you have a working Web site that shows all the information related to categories and products available in the site. Imagine, for example, one of the partners of ShoppingAssistant site wants to find out the number of times a particular product has been viewed by the users. To accomplish this, you need to be able to store the details of all the products that are displayed on the product details page. If you were to handle this process of storing data in a synchronous way such as making a...
Listing Serializing DataSet to XML Using WriteXml
lt Page Language C gt lt Import lt Import Namespace System.Data gt lt Import lt Import Namespace System.IO gt lt script runat server gt void Page_Load Object sender, EventArgs e string filePath Check if the file exists in the hard drive Read the contents of the XML file into the DataSet contactsDataSet.ReadXml filePath contactsDataSet GetContactTypes Write the contents of the DataSet to a local XML file contactsDataSet.WriteXml filePath gridContacts.DataSource contactsDataSet.Tables 0...
Role of XML Schemas in Typed DataSets
A System.Data.DataSet can either be typed or untyped. A typed DataSet is a class that is derived from a DataSet class and has an associated XML schema. On the other hand, an untyped DataSet does not have an XML schema associated with it. In a typed DataSet, you can make changes to the XSD file, which are reflected in the underlying DataSet. XML schema is similar to the typed DataSet representation because both are available as XSD files in the XML designer in Visual Studio however, a typed...
XmlWriter Classs Namespace Support
In the XmlWriter class, all the methods available for writing element nodes and attributes have overloads to work with namespaces. You simply add a new argument to the call and specify the namespace prefix of choice. You insert a namespace declaration in the current node using the xmlns attribute. You can also optionally specify a namespace prefix. The prefix is a symbolic name that uniquely identifies the namespace. A namespace is identified by a URN and is used to qualify both attribute and...
The ReadXmlSchema Method
You use the ReadXmlSchema method when you want to load only DataSet schema information and no data from an XML document. This method loads a DataSet schema using the XSD schema. The ReadXmlSchema method takes a stream, an XmlReader, or a file name as a parameter. In the event of absence of an inline schema in the XML document, the ReadXmlSchema method interprets the schema from the elements in the XML document. When you use the ReadXmlSchema method to load a DataSet that already contains a...
Asynchronous Execution of FOR XML Query
In the previous section, you saw how to synchronously execute a FOR XML query using the ExecuteXmlReader method. Although this approach works, there are times where you might want to execute the query asynchronously for scalability and throughput reasons. Fortunately ADO.NET 2.0 comes shipped with a new feature that provides for asynchronous execution of SQL commands. Using this new feature, you can now asynchronously execute commands against a SQL Server database without waiting for the...
Reading an XML File Using XmlReader
Now that you know the theory, this section begins with an example to demonstrate how to read an XML document using an XmlReader object. This simple example leverages the functionalities of the XmlReader class to parse a static XML file named Employees.xml. Here's the XML file, a list of employees in an organization, shown in Listing 4-1. lt xml version '1.0' gt lt employees gt lt employee id 1 gt lt name gt lt name gt lt city gt Seattle lt city gt lt state gt WA lt state gt lt zipCode gt 98122...
Listing Validating XML Data Using XmlSchemaSet Class
lt Page Language C gt lt Import Namespace System.Xml gt lt Import Namespace System.Xml.Schema gt lt script runat server gt private StringBuilder _builder new StringBuilder void Page_Load object sender, EventArgs e string xmlPath Request.PhysicalApplicationPath App_Data Authors.xml string xsdPath Request.PhysicalApplicationPath App_Data Authors.xsd XmlSchemaSet schemaSet new XmlSchemaSet schemaSet.Add null, xsdPath XmlReader reader null XmlReaderSettings settings new XmlReaderSettings...
Working with XML Data Type Columns from ADONET
You get your first indication that XML is now a first class relational database type by referencing the relational data type enumerations in ADO.NET 2.0. System.Data.DbType and System.Data.SqlDbType contain additional values for DbType.Xml and SqlDbType.Xml, respectively. There is also a new class called SqlXml that is contained in the System.Data.SqlTypes namespace, and this class acts as a factory class for creating XmlReader instances on top of the XML type value. You can access the XML...
XLink and XPointer
It's hard to imagine the World Wide Web without hyperlinks, and, of course, HTML documents excel at letting you link from one to another. How about XML In XML, it turns out, you use XLinks and XPointers. XLinks enables any element to become a link, not just a single element as with the HTML lt A gt element. That's a good thing because XML doesn't have a built-in lt A gt element. In XML, you define your own elements, and it only makes sense that you can define which of those represent links to...
The XsltSettings Class
This new class introduced with .NET Framework 2.0 allows you to specify the XSLT features to support during execution of the XSLT style sheet. For example, embedding script blocks and the use of XSLT document function are optional features on the XslCompiledTransform class that are disabled by default. Enabling these features requires you to create an instance of the XsltSettings class, set appropriate properties of that object, and supply that as an input to the Load method of the...
Programmatically Transforming Data Using Xml Control
The previous example demonstrated the steps involved in using static XML files and XSL files as an input to the Xml control. This section will provide an example that shows how easy it is to consume and display an RSS 2.0 feed with ASP.NET. Specifically the ASP.NET XML control will be used to transform an RSS feed into a presentable format on a Web page. First look at the structure of the RSS 2.0 XML file shown in Listing 9-15. Listing 9-15 RSS.XML File lt xml version 1.0 gt lt rss version 2.0...
Listing Programmatically Transforming XML Data using Xml Control
lt Page Language C gt lt Import Namespace System.Xml gt lt Import Namespace System.Xml.Xsl gt lt script runat server gt private void Page_Load object sender, EventArgs e XmlDocument doc new XmlDocument Xmll.Document doc Xmll.TransformSource App_Data RSS.xsl lt html gt lt head runat server gt lt title gt Programmatically Displaying XML Data using Xml Web Server Control lt title gt lt head gt lt body STYLE font-family Arial, helvetica, sans-serif font-size 12pt background-color EEEEEE gt lt form...
The WebServiceBinding Attribute
With the .NET 1.x release, you can build services that conform to the Basic Profile today by following the guidance in Building Interoperable Web Services however .NET 2.0 makes it even easier to build Web services that conform to the Basic Profile 1.0. To accomplish this, you need to add the WebServiceBinding attribute that allows you to build a WS-I Basic Profile conformant Web service. Setting WebServiceBinding.ConformsTo to WsiProfiles.BasicProfile1_1 makes the SOAP 1.1 port exposed by this...
Pregenerating Serialization Assemblies
The XML serializer is a powerful tool that can transform a fair number of .NET Framework classes into portable XML code. The key thing to note is that the serializer behaves similar to a compiler. It first imports type information from the class and then serializes it to the output stream. It also works the other way around. The serializer reads XML data and maps elements to the target class members. Because each of these classes is unique in certain way, it is impossible for a generic tool to...
Using Asynchronous Invocation of Web Services and Windows Service
This section discusses how to substitute the synchronous Web service invocation approach with the asynchronous approach that would greatly improve the throughput of the pages. The idea here is to get the data from the remote Web services in asynchronous manner and then store that information as XML files in a local folder. After the information is available in the XML files, the ShoppingAssistant Web pages can retrieve the required information from local XML files for display purposes. To...
Performance Optimization with XPathNavigator
Another way to use XPath to query your XML data is to create and use an XPathNavigator object. The XPathNavigator class, in conjunction with the other classes in the System.Xml.XPath namespace such as XPathDocument, XPathExpression, and the XPathNodelterator, enables you to optimize performance when working with XPath queries. The XPathNavigator class has methods such as Select, Compile, and Evaluate to perform queries on your XML data by using XPath expressions. Among all the other things that...
Passing Parameters to an XML Data Type Column
So far, you have seen how to retrieve an XML data type column from the client ASP.NET page. This section focuses on how to pass parameters to an XML data type column. As similar to the retrieval, here also you have two options in terms of choosing the appropriate parameter data type. Use NVarChar as the SqlDbType and rely on the automatic conversion wherein SQL Server automatically typecasts the parameter to XML data type The next few sections examine both of these approaches in detail. Passing...
X
XDR XML Data Reduced , 13,100 XForms, 8 XHTML Extensible Hypertext Markup Language , 8 XhtmlConformanceSection class, 471 XLink, 8, 20 Xml control DocumentContent property, 265 DocumentSource property, 265 programmatically transforming data using, 269-271 RSS 2.0 XML file, 269-270 Transform property, 265 TransformArgumentList property, 265 TransformSource property, 265 XML Data Reduced XDR , 13,100 XML data type and DataSet object, 326-329 exist method, 302 indexing XML columns, 302-303...
Creating a Custom Configuration Section
There are times where you may have to create a custom configuration section and add it to a configuration file at runtime. For example, if you are creating a Windows installer for deploying a Web application and you need to create sections of the web.config file dynamically, you need to be able to create and persist a custom section at runtime. With ASP.NET 2.0, you now have an easy and effective way to accomplish this. You accomplish this by going through the following steps 1. Create a custom...


