A.A. XML


XML
XML is Extensible Markup Language there shorten form is xml, you can learn about xml language very easy ,there are several ways to learn about xml but I tried to teach you easy way for understanding,you can learn ,
2Introduction to XML,3Introduction to XML , A Introduction to XML , A Primer on XML , An Introduction to ASP.NET 2.0 XML , An Introduction to XML ,and Entity References XML , ASP.NET 2.0 Configuration XML , Attributes XML ,Building XML a ShoppingAssistant using XML Web Services (1) Building an Airline Reservation System using ASP.NET 2.0 XML , CDATA XML, CODING XML , Components of an XML Document (1) CREATING XML (1)Data Controls (1) Document Classes XML (1) Elements (1) Extension Objects XML (1) How Does XSLT Work? (1) Implementing the Server-Side Event for Callback XML (1) In-Depth Look at Schemas (1) In-Process Access to the XML Data Type Column (1) Indexing XML Columns (1) Inserting Data into an XML Column (1) Introduction XML CODE (1) Introduction to ASP.NET 2.0 (2) Introduction to XML (2) more oihers (1) PCDATA (1) Programming with the XML Document Object Model (2) Reading and Writing XML (2) Reading and Writing XML Data Using XmlReader and XmlWriter (2) Self-Describing Data (1) SQL Server 2005 XML Integration (2) Summary XML (1) Symbol XML (1) Transforming XML Data with XSLT (2) Understanding XML Validation (1) Untyped XML Columns (1) User Defined Functions in an XSL Style Sheet XML (1) Validating and Non-Validating Parsers (1) Well-Formed (1) What This site Covers WITH XML (1) What You Need to Use This site (1) Working with XmlDocument Class (1) Writing XML (1) XML 3 (1) XML ASP.NET 2.0 Suppor t for Accessing Configuration Settings XML (1) XML ASP.NET Configuration (1) XML Classes in the .NET Framework (4) XML Data Display (3) XML Data Type in SQL Server 2005 (1) XML Declaring Namespaces (1) XML DOM (1) XML DOM Object Model (1) XML Introduction (1) XML Namespaces (1) XML Serialization (2) XML SQL Server 2005 XML Integration2 (1) XML Support in ADO.NET (1) XML Support in the .NET Framework 2.0 (1) XML Technologies XML (1) XML Web Services (2) XQuery (1) XSD AND XML (1) XSLT in .NET Framework XML 2.0
   Also you can…..
               Learn Xml,xml learn,xml file,xml books,using xml,sax xml,xml element,learn xml,xml node,xml write,reading xml,xml xpath,xml sax,xml validator,rdf xml,xml mapping,xml metadata,xml xsd,and other xml or xml language,,,…….


learn guitarphysics learnteliphonyxmlphysicsenjoylife

XML Serialization




XML was designed to be a technology for data exchange across heterogeneous systems. You can
easily transmit XML between distributed components because of its platform independence and
its simple, text-based, self-describing format, yet these features hardly form the basis for a solid
programming platform. Text-based data does not enforce type-safety rules. Programmers are much
more enticed by object-oriented programming models because each object is of a certain type, so
the compiler can warn of potential type problems, and data encapsulated by an object can be eas-
ily accessed. The ideal programming environment would use an object-oriented model to build
the software but leverage the benefits of XML to communicate between distributed components,
over the Internet or Message Queues, for example. This is where XML serialization plays an
important role by providing you with the bridge that enables you to seamlessly transform an
object to XML and vice versa.

XML serialization is the process of translating a collection of data into a stream of information.
Deserialization is the reverse: translating a stream of information back into the data that originally
produced it. Sometimes these processes are called dehydration and rehydration. The System.Xml
.Serialization namespace contains classes to create an XML representation for an object or ini-
tialize an object directly from XML. Using XML serialization will reduce the amount of code you
have to develop for an XML-based data exchange application. You no longer have to parse XML
to initialize objects, neither do you have to develop code for objects to persist themselves to XML.
After you define what the XML format you use to exchange data looks like, you can quickly
develop classes that can automatically store their data to the XML format or objects can be auto-
matically created from XML.

This chapter focuses on the serialization features of .NET framework that are used to serialize
objects to an XML-based representation and then deserializing the XML back into objects. You also
learn how to customize the output generated by the serialization of objects so their XML represen-
tation will map to a given XML format. By the end of this chapter, you will have a good under-
standing of the following:

  XML serialization

  How to serialize an object into an XML format


learn guitarphysics learnteliphonyxmlphysicsenjoylife

SQL Server 2005 XML Integration2




using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{

 [Microsoft.SqlServer.Server.SqlProcedure]
public static void GetEmployeeNameByID(int id)
{

 string retValue = “”;
using (SqlConnection conn = new

SqlConnection(“context connection=true”))

{

 conn.Open();
//Prepare query to select xml data
SqlCommand cmd = conn.CreateCommand();
string sql = “SELECT xml_data.query “ +

“(‘declare namespace ns=\”http://www.wrox.com/books\”;” +
“ <Employee Name=\”{/ns:employee/ns:name}\”/>’) as Result “ +
“ FROM Employee WHERE id = “ + id.ToString();

cmd.CommandText = sql;
//Execute query and retrieve incoming data
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{

 //Get the XML value as string
retValue = (string)reader.GetValue(0);

}
else

retValue = “No Value”;

}
//Send the output XML back to the caller
SqlContext.Pipe.Send(retValue);

}

};

The in-proc provider is optimized for working with data inside the SQL Server process. Using the classes
and methods of the in-process managed provider, you can easily submit queries to the database, execute
DML and DDL statements, and return result sets and messages to client applications. The Microsoft
.Data.SqlServer namespace groups the types that make up the in-proc provider. This namespace
shares many similarities and interfaces with ADO.NET’s SqlClient namespace, which is used by devel-
opers accessing SQL Server data from managed client and middle-tier applications. Because of this simi-
larity, you can easily migrate code from client applications to server libraries and back again.

There are three important classes in the Microsoft.SqlServer.Server namespace that are specific to
the in-proc provider:

  SqlContext —This class encapsulates the other extensions. In addition it provides the transac-

tion and database connection, which are part of the environment in which the routine executes

  SqlPipe —This class enables routines to send tabular results and messages to the client. This

class is conceptually similar to the Response class found in ASP.NET in that it can be used to
send messages to the callers.

Now that you have an understanding of the important classes, walk through the code of Listing 10-3.


learn guitarphysics learnteliphonyxmlphysicsenjoylife

In-Process Access to the XML Data Type Column




Before looking at an example, it is important to understand the CLR integration features of SQL Server
2005. One of the excellent features of SQL Server 2005 is the integration with the .NET CLR (Common
Language Runtime). The integration of CLR with SQL Server 2005 extends the capability of SQL Server
in several important ways. In previous versions of SQL Server, database programmers were limited to
using T-SQL when writing code on the server side. With CLR integration, database developers can now
perform tasks that were impossible or difficult to achieve with Transact-SQL alone. Both Visual Basic
.NET and C# are modern programming languages offering full support for arrays, structured exception
handling, and collections. Developers can leverage CLR integration to write code that has more complex
logic and is more suited for computation tasks using languages such as VB.NET and C#. Both VB.NET
and C# offer object-oriented capabilities such as encapsulation, inheritance, and polymorphism.

Advantages of CLR Integration

Managed code is better suited than Transact-SQL for number crunching and compli-
cated execution logic, and features extensive support for many complex tasks,
including string handling and regular expressions. With the functionality found in
the .NET Framework Base Class Library (BCL), database developers now have access
to thousands of pre-built classes and routines which can be easily accessed from any
stored procedure, trigger, or user-defined function. The BCL includes classes that
provide functionality for improved string functioning, advanced math operations,
file access, cryptography, and more. Although many of these classes are available for
use from within SQL CLR code, those that are not appropriate for server-side use
(for example, windowing classes) are not available.
Another benefit of managed code is type safety. Before managed code is executed,
the CLR verifies that the code is safe. This process is known as “verification.”
During verification, the CLR performs several checks to ensure that the code is safe
to run. For example, the code is checked to ensure that no memory is read that has
not been written to. The CLR will also prevent buffer overflows. By default, both
Visual Basic .NET and C# always produce safe code; however, C# programmers have
the option of using the unsafe keyword to produce unsafe code that, for example,
directly accesses memory.

For the purposes of this example, consider the

Employee table that has been used in the previous
examples.

CREATE TABLE Employee (id int primary key, xml_data xml( EmployeeSchema))

Listing 10-3 illustrates how the XML data type can be accessed from the in-proc provider. The context
connection allows you to execute SQL statements in the same context that the CLR code was invoked.
For out-of-proc access, a new connection to the database must be established.

Listing 10-3: Accessing an XML Data Type Column Using In-Proc

using System;
using System.Data;

using System.Data.SqlClient;
learn guitarphysics learnteliphonyxmlphysicsenjoylife

SQL Server 2005 XML Integration




Primary XML Index

This indexes all tags, values, and paths within the XML instances in an XML column. The base table (that
is, the table in which the XML column occurs) must have a clustered index on the primary key of the
table. The primary key is used to correlate index rows with the rows in the base table. The following
statement creates a primary XML index called idx_xml_data on the XML column xml_data of the table
Employee:

CREATE PRIMARY XML INDEX idx_xml_data on Employee (xml_data)

Secondary XML Indexes

After the primary XML index has been created, you may want to create secondary XML indexes to speed
up different classes of queries within your workload. There are three types of secondary XML indexes
named PATH, PROPERTY, and VALUE that can benefit path-based queries, custom property management
scenarios, and value-based queries, respectively.

The PATH index builds a B+-tree on the (path, value) pair of each XML node in document order over all
XML instances in the column. The PROPERTY index creates a B+-tree clustered on the (PK, path,
value) pair within each XML instance, where PK is the primary key of the base table. Finally, the VALUE

index creates a B+-tree on the (value, path) pair of each node in document order across all XML
instances in the XML column.

If your workload uses path expressions heavily on XML columns, the PATH sec-
ondary XML index is likely to speed up your workload. If your workload retrieves
multiple values from individual XML instances using path expressions, clustering
paths within each XML instance in the PROPERTY index may be helpful. If your
workload involves querying for values within XML instances without knowing the
element or attribute names that contain those values, you may want to create the

VALUE index.

To create a PATH index on the xml_data column, use the following command.

CREATE XML INDEX idx_xml_data_path on Employee (xml_data)

USING XML INDEX idx_xml_data FOR PATH


learn guitarphysics learnteliphonyxmlphysicsenjoylife

Indexing XML Columns




XML indexes can be created on XML data type columns. It indexes all tags, values, and paths over the
XML instances in the column and can result in improved query performance. XML indexing can be very
useful in the following scenarios.

  When there is a need to frequently execute queries on XML columns.

  When the values you retrieve from XML values are relatively small compared to the size of the

XML column itself. By indexing that XML column, you can avoid parsing the whole data at run-
time and be benefited by index lookups for efficient query processing.

There are two types of indexes that can be created on an XML column. They are primary XML index and
secondary XML index. As the name suggests, the first index on an XML column is the primary XML
index. Using it, three types of secondary XML indexes can be created on the XML column to speed up

common classes of queries.
learn guitarphysics learnteliphonyxmlphysicsenjoylife

Inserting Data into an XML Column




Irrespective of whether the XML column is typed or not typed, you can supply the value for an XML col-
umn in the following ways.

  As a character or binary SQL type that is implicitly converted to XML data type.

  As the content of a file.

  As the output of the FOR XML with the TYPE directive that generates an XML data type instance.

The supplied value is checked for well-formedness and allows both XML documents and XML fragments
to be stored. If the data fails the well-formedness check, it is rejected with an appropriate error message.
For typed XML, the supplied value is checked for conformance to XML schemas registered with the
XML schema collection typing the XML column. The XML instance is rejected if it fails this validation.
Look at examples on the different ways of inserting values into an XML column.

To start with, the following statement inserts a new row into the Employee table with the value 1 for the
integer column ID and an <employee> instance for the xml_data column. The <employee> data, supplied
as a string, is implicitly converted to XML data type and checked for well-formedness during insertion.

INSERT INTO Employee values (2, ‘<employee id=”2”><name>Joe</name></employee>’)

It is also possible to utilize the contents of an XML file as an input to the Insert command. Consider the
following XML document stored in a file called Employee.xml.

<employee id=”6” xmlns=”http://www.wrox.com/books”>

<name>Dave</name>

</employee>

Now if you execute the following T-SQL command, you will see the contents of the Employee.xml file
being loaded into the xml_data column.

INSERT INTO Employee SELECT 7, xml_value FROM   

(SELECT * FROM OPENROWSET (BULK ‘C:\Data\Employee.xml’,
SINGLE_BLOB) AS xml_value) AS R(xml_value)

The third option is to utilize the output of the FOR XML with the TYPE directive as an input to the insert
command. With SQL Server 2005 FOR XML has been enhanced with a TYPE directive to generate the result
as an XML data type instance. The resulting XML can be assigned to an XML column, variable, or parame-
ter. In the following statement, the XML instance generated using FOR XML TYPE is assigned to an XML
data type variable @var. Then the variable is used in the insert statement.

DECLARE @var xml
SET @var = (SELECT xml_data FROM Employee FOR XML AUTO,TYPE)
--Insert the value of the variable into a new table named EmployeeOutput
CREATE TABLE EmployeeOutput (xml_data xml)
INSERT INTO EmployeeOutput (xml_data) VALUES (@var)


learn guitarphysics learnteliphonyxmlphysicsenjoylife