.NET Data Provider for Teradata: Sending and Receiving XML

Blog
The best minds from Teradata, our partners, and customers blog about whatever takes their fancy.
Teradata Employee

The XML data type was released in Teradata Database 14.10.  This type enables XML documents and fragments to be stored in a column defined as XML.  Support for the XML type was also added to the  .NET Data Provider for Teradata 14.10 release. 

TdXml is the provider specific type used to support XML by the Data Provider.   It is used to retrieve XML documents and fragments from a Teradata Database.  A TdXml instance is created by calling TdDataReader.GetTdXml() on the XML column.  After the instance is created, the method TdXml.CreateXmlReader() is called to create an XmlReader instance.  The XmlReader is used to traverse the XML document or fragment.

 Here is an example of retrieving an XML document:

Public void RetrieveXmlExample(TdCommand cmd)
{
    cmd.Parameters.Clear();

    cmd.CommandText = "select XmlColumn1 from TableExample";

    using (TdDataReader reader = cmd.ExecuteReader())
    {
        // read a row returned from Teradata
        while (reader.Read() == true)
        {
             // retrieve the Xml data
             //      with the Teradata Database.
             using (TdXml tXml = reader.GetTdXml(0))
             {
                 // Creating an XmlReader so that the Xml can be traversed
                 using (XmlReader xml = tXml.CreateXmlReader())
                 {
                     //
                     // traversing of the Xml is performed by making call to the XmlReader methods.
                     //
                 }              
             }
         }
    }
}

It is important that the TdXml instance (tXml) gets disposed after all the data has been processed.  By disposing this instance, all the Teradata resources that were allocated by the Data Provider are released.  It is the responsibility of the application to dispose the XmlReader instance that was returned from the TdXml.CreateXmlReader ().

XML can be sent to a Teradata Database (when inserting and updating) using an XmlReader or TextReader object.  The following is an example that uses an XmlReader to insert data:

Public void InsertXml(TdCommand cmd)
{
    cmd.Parameters.Clear();

    cmd.CommandText = "INSERT INTO TableExample (colInteger, colXml1, colXml2) values (?, ?, ?)";

    // creating the first parameter
    cmd.Parameters.Add(null, TdType.Integer);
    cmd.Parameters[0].Direction = Parameter.Direction.Input;
    cmd.Parameters[0].Value = 100;

    // creating the Xml parameter -- an XmlReader is used
    // XmlTextReader inherits from XmlReader
    XmlTextReader xmlReader = new XmlTextReader(@"c:\XmlDocFile1.xml");
    cmd.Parameters.Add(null. TdType.Xml);
    cmd.Parameters[1].Direction = ParameterDirection.Input;
    cmd.Parameters[1].Value = xmlReader;

    // creating the Xml parameter -- an TextReader is used.
    // StreamReader inherits from TextReader
    StreamReader xmlStream =  new StreamReader(@"c:\XmlDocFile2.xml");
    cmd.Parameters.Add(null. TdType.Xml);
    cmd.Parameters[2].Direction = ParameterDirection.Input;
    cmd.Parameters[2].Value = xmlStream;

    try
    {
        cmd.ExecuteNonQuery() ;
    }
    finally
    {
        xmlReader.Close();
        xmlStream.Close();
    }
}