Exemplo n.º 1
0
        public static SifElement WriteParseAndReturn( 
            SifElement o,
            SifVersion version,
            SchemaValidator validator, 
            Boolean echoOut )
        {
            SifElement returnVal;

            if ( o is SifMessagePayload )
            {
                o.SifVersion = version;
            }

            SifWriter echo = null;

            if ( echoOut )
            {
                //   Write the object to System.out
                Console.WriteLine( "Writing object : " + o.ElementDef.Name
                                   + " using SIFVersion: " + version.ToString() );

                echo = new SifWriter( Console.Out );
                echo.Write( o, version );
                echo.Flush();
                Console.Out.Flush();
                
            }

            //  Write the object to a file
            Console.WriteLine( "Writing to file... test.xml" );
            using (Stream fos = new FileStream("test.xml", FileMode.Create))
            {
                SifWriter writer = new SifWriter( new StreamWriter( fos, Encoding.UTF8 ) );
                o.SetChanged( true );
                writer.Write( o, version );
                writer.Flush();
                writer.Close();
                fos.Close();
            }

            if ( validator != null )
            {
                Validate( "test.xml", validator );
            }

            //  Parse the object from the file
            Console.WriteLine( "Parsing from file..." );
            SifParser p = SifParser.NewInstance();

            FileStream fr = new FileStream( "test.xml", FileMode.Open );

            StreamReader inStream = new StreamReader( fr, Encoding.UTF8 );


            returnVal = p.Parse( inStream, null, 0, version );

            inStream.Close();
            fr.Close();
            //  Write the parsed object to System.out
            returnVal.SetChanged( true );
            Console.WriteLine( "Read object : " + returnVal.ElementDef.Name );
            if ( echoOut )
            {
                echo.Write( returnVal, version );
                echo.Flush();
            }


            return returnVal;
        }