毛毛的小窝 — 关注技术交流、让我们一起成长

导航

(原) ODP.NET 演示 OracleXmlStream 的属性和方法

/*
 * 1. OracleXmlType objects can be used for well-formed XML documents with or without XML schemas or XML fragments. 
 * 2. An OracleXmlStream object represents a read-only stream of XML data stored in an OracleXmlType object. 
 * 3. OracleXmlStream 和 OracleXmlType 的用途还没有完全弄清楚。。。
*/


namespace XMLStreamProp11
{
    
using System;
    
using System.Data;
    
using System.Text;
    
using Oracle.DataAccess.Client;
    
using Oracle.DataAccess.Types;

    
// 演示 OracleXmlStream 的属性和方法
    class Program
    
{
        
static void Main(string[] args)
        
{
            
// Create the connection.
            string constr = "User Id=scott;Password=tiger;Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.24)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = bjoracle.oracle10g.mynet)))";
            OracleConnection con 
= new OracleConnection(constr);
            con.Open();

            
// Create an OracleXmlType from String
            StringBuilder blr = new StringBuilder();
            blr.Append(
"<?xml version=\"1.0\"?> <PO pono=\"1\"");
            blr.Append(
"<PNAME>Po_1</PNAME> <CUSTNAME>John</CUSTNAME> ");
            blr.Append(
"<SHIPADDR> <STREET>1033, Main Street</STREET> ");
            blr.Append(
"<CITY>Sunnyvale</CITY> <STATE>CA</STATE> </SHIPADDR> ");
            blr.Append(
"</PO>");

            
//Create a OracleXmlStream from OracleXmlType
            OracleXmlType xml = new OracleXmlType(con, blr.ToString());
            OracleXmlStream strm 
= new OracleXmlStream(xml);

            
// Print the length of xml data in the stream.
            Console.WriteLine("OracleXmlStream Length: " + strm.Length);
            Console.WriteLine(
"");

            
// Print the xml data in the stream
            Console.WriteLine("OracleXmlStream Value: " + strm.Value);
            Console.WriteLine(
"");

            
// Check CanRead property on the stream
            Console.WriteLine("OracleXmlStream CanRead: " + strm.CanRead);
            Console.WriteLine(
"");

            
// Check CanWrite property on the stream.
            Console.WriteLine("OracleXmlStream Can Write: {0}\n", strm.CanWrite);

            
// Read 10 bytes at a time from the stream.
            int rb = 0;
            
int curpos = 0;
            
byte[] bytebuf = new byte[500];
            
while ((rb = strm.Read(bytebuf, curpos, 10)) > 0)
                curpos 
+= rb;

            
// Print the contents of the byte array.
            System.Text.Encoding encoding = System.Text.Encoding.Unicode;
            Console.WriteLine(
"OracleXmlStream Read byte[]: " + encoding.GetString(bytebuf));
            Console.WriteLine(
"");

            
// Print current position in stream.
            Console.WriteLine("OracleXmlStream Position: " + strm.Position);
            Console.WriteLine(
"");
            strm.Dispose();
            xml.Dispose();
            con.Close();
            con.Dispose();

            
// Waiting
            Console.ReadLine();
        }

    }

}

posted on 2007-12-29 21:51  mjgforever  阅读(358)  评论(0编辑  收藏  举报