Wednesday, November 30, 2005

Validating XML with XSD


public bool VerifySchema(string xmlFilePath, string strXSDPath)
{
bool IsSucessful = true;
try
{
XmlTextReader r = new XmlTextReader("D:\\usr\\PRASANTH\\VCPP\\COMPlus\\06-10-2005T103217726-EMRGDIARY.xml");
XmlValidatingReader v = new XmlValidatingReader(r);
XmlSchemaCollection xsc = null;
XmlTextReader tr ;
tr = new XmlTextReader(strXSDPath);
xsc = new XmlSchemaCollection();
xsc.Add(null, tr);
v.Schemas.Add (xsc);
v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler +=
new ValidationEventHandler(MyValidationEventHandler);
while (v.Read())
{
}
v.Close();
// Check whether the document is valid or invalid.
if (isValid)
Console.WriteLine("Document is valid");
else
Console.WriteLine("Document is invalid");
}
catch (Exception ex)
{
IsSucessful = false;
throw new Exception (ex.Message);
}
return IsSucessful;
}
///
/// Validation handler for the validation errors in the XML file
///

///
///
public static void MyValidationEventHandler(object sender, ValidationEventArgs args)
{
isValid = false;
Console.WriteLine("Validation event\n" + args.Message);
}

Tuesday, November 29, 2005

Stored Procedures for the xml insertion

CREATE TABLE RDCXMLFILES ( FILENAME VARCHAR2 (50), FILECONTENT XMLTYPE ) ;

CREATE OR REPLACE PROCEDURE spT1 (tmpBlob in out CLOB)ASdpBlob CLOB; BEGINDBMS_LOB.CREATETEMPORARY(dpBlob, False, 0);tmpBlob := dpBlob; END;/

create or replace procedure spInsertRDCXML(File_Name in varchar, File_Content in CLOB)ASv_FileName varchar(26);v_doc CLOB;v_xml XMLTYPE;BEGINv_FileName := File_Name;v_doc := File_Content ;v_xml := sys.xmltype.createXML(v_doc);INSERT INTO RDCXMLFILES VALUES (v_FileName, v_xml );END;/

XML Insertions for Oracle XMLType Large Files

private void MethodXX()
{
try
{
string fileName = "File.DAT";
StreamReader objSR = new StreamReader (@"..\\..\\File.DAT");
string completeFile = objSR.ReadToEnd ();
OracleConnection oraCon = new OracleConnection ();
oraCon.ConnectionString = "Data Source=RDCFEED;User Id=SYSMAN;Password=password;Integrated Security=no;";
oraCon.Open ();
OracleCommand oraCmd = new OracleCommand ("spT1", oraCon);
oraCmd.Transaction = oraCmd.Connection.BeginTransaction ();
oraCmd.CommandType = CommandType.StoredProcedure;
oraCmd.Parameters.Add(new OracleParameter("tmpBlob", OracleType.Clob)).Direction = ParameterDirection.Output;
oraCmd.ExecuteNonQuery();
OracleLob tempLob = OracleLob.Null ;
tempLob = (OracleLob )oraCmd.Parameters[0].Value;
byte [] fileInBytes;
Encoding temp = Encoding.Unicode ;
fileInBytes = temp.GetBytes (completeFile);
tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
tempLob.Write(fileInBytes, 0, fileInBytes.Length);
tempLob.EndBatch();
oraCmd.Parameters.Clear();
oraCmd.CommandText = "spInsertRDCXML";
oraCmd.CommandType = CommandType.StoredProcedure;
OracleParameter paramFileName = new OracleParameter ("File_Name", OracleType.VarChar);
paramFileName.Direction = ParameterDirection.Input ;
paramFileName.Value = fileName;
OracleParameter paramFileContent = new OracleParameter ("File_Content", OracleType.Clob);
paramFileContent.Direction = ParameterDirection.Input ;
paramFileContent.Value = tempLob;
oraCmd.Parameters.Add (paramFileName);
oraCmd.Parameters.Add (paramFileContent);
oraCmd.ExecuteNonQuery ();
oraCmd.Transaction.Commit ();
}
catch(Exception ex)
{
MessageBox.Show (ex.Message );
}
}