XmlTextWriter.WriteBase64 メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlTextWriter.WriteBase64 メソッドの意味・解説 

XmlTextWriter.WriteBase64 メソッド

指定したバイナリ バイトbase64 としてエンコードし、その結果生成されるテキスト書き込みます

名前空間: System.Xml
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

buffernull 参照 (Visual Basic では Nothing) です。

ArgumentException

バッファ長から index差し引いた値が count より小さい値です。

ArgumentOutOfRangeException

index または count が 0 未満です。

InvalidOperationException

WriteState が Closed です。

解説解説
使用例使用例

WriteBase64使用して入力ファイルエンコードし、一時 XML ファイル生成する例を次に示します一時 XML ファイルは、ReadBase64 メソッド使用してデコードされ、元のファイル比較されます。

Imports System
Imports System.IO
Imports System.Xml
Imports System.Text

class TestBase64 

    private const bufferSize as
 integer = 4096

    public shared sub Main()
 
 
        Dim args() As String
 = System.Environment.GetCommandLineArgs()
        Dim myTestBase64 as TestBase64 = new
 TestBase64()
        
        'Check that the usage string is correct.
        if (args.Length < 3 ) 
           myTestBase64.Usage()
           return
        end if

        Dim fileOld as FileStream = new
 FileStream(args(1), FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
        myTestBase64.EncodeXmlFile("temp.xml", fileOld)

        Dim fileNew as FileStream = new
 FileStream(args(2), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)

        myTestBase64.DecodeOrignalObject("temp.xml",
 fileNew)

        'Compare the two files.
        if (myTestBase64.CompareResult(fileOld, fileNew))
            Console.WriteLine("The recreated binary file {0} is
 the same as {1}", args(2), args(1))
        else 
            Console.WriteLine("The recreated binary file {0} is
 not the same as {1}", args(2), args(1))
        end if

        fileOld.Flush()
        fileNew.Flush()
        fileOld.Close()
        fileNew.Close()

    end sub

    'Use the WriteBase64 method to create an XML document.  The object
  
    'passed by the user is encoded and included in the document.
    public shared sub EncodeXmlFile(xmlFileName
 as String, fileOld as
 FileStream) 

        Dim buffer(bufferSize) as byte
        Dim readByte as integer=0

        Dim xw as XmlTextWriter = new
 XmlTextWriter(xmlFileName, Encoding.UTF8)
        xw.WriteStartDocument()
        xw.WriteStartElement("root")
        ' Create a Char writer.
        Dim br as BinaryReader = new
 BinaryReader(fileOld)
        ' Set the file pointer to the end.

        try 
              do 
                  readByte=br.Read(buffer, 0, bufferSize)
                  xw.WriteBase64(buffer, 0, readByte)
              loop while (bufferSize <=
 readByte )

        catch ex as Exception
            Dim ex1 as EndOfStreamException
 = new EndOfStreamException()

            if (ex1.Equals(ex)) 
                Console.WriteLine("We are at end of file")
            else 
                Console.WriteLine(ex)
            end if
        end try
        xw.WriteEndElement()
        xw.WriteEndDocument()

        xw.Flush()
        xw.Close()
    end sub

    'Use the ReadBase64 method to decode the new XML document 
    'and generate the original object.
    public shared sub DecodeOrignalObject(xmlFileName
 as String, fileNew as
 FileStream) 

        Dim buffer(bufferSize) as byte
        Dim readByte as integer
 =0

        'Create a file to write the bmp back.
        Dim bw as BinaryWriter = new
 BinaryWriter(fileNew)

        Dim tr as XmlTextReader = new
 XmlTextReader(xmlFileName)
        tr.MoveToContent()
        Console.WriteLine(tr.Name)

        do 
          readByte=tr.ReadBase64(buffer, 0, bufferSize)
          bw.Write(buffer, 0, readByte)
        loop while(readByte>=bufferSize)

        bw.Flush()

    end sub

    'Compare the two files.
    public function CompareResult(fileOld as
 FileStream, fileNew as FileStream) as boolean

        Dim readByteOld as integer=0
        Dim readByteNew as integer=0
        Dim count as integer
        Dim readByte as integer=0

        Dim bufferOld(bufferSize) as byte
        Dim bufferNew(bufferSize) as byte

        Dim binaryReaderOld as BinaryReader
 = new BinaryReader(fileOld)
        Dim binaryReaderNew as BinaryReader
 = new BinaryReader(fileNew)

        binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin)
        binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin)

        do 
          readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize)
          readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize)

          if not (readByteOld=readByteNew)
 
              return false
          end if

          for count=0 to bufferSize-1
              if not (bufferOld(count)=bufferNew(count))
 
                  return false
              end if
          next

        loop while (count<readByte)
        return true
    end function

    'Display the usage statement.
    public shared sub Usage()
        Console.WriteLine("TestBase64 sourceFile, targetFile")
        Console.WriteLine("For example: TestBase64 winlogon.bmp,
 target.bmp")
    end sub

end class
using System;
using System.IO;
using System.Xml;
using System.Text;

class TestBase64 {

    private const int bufferSize=4096;

    public static void Main(String[]
 args) {

        TestBase64 testBase64=new TestBase64();

        //Check that the usage string is correct.
        if (args.Length < 2 ) {
            testBase64.Usage();
            return;
        }

        FileStream fileOld = new FileStream(args[0], FileMode.OpenOrCreate
,
 FileAccess.Read, FileShare.Read);
        testBase64.EncodeXmlFile("temp.xml", fileOld);

        FileStream fileNew = new FileStream(args[1], FileMode.Create
,
 FileAccess.ReadWrite, FileShare.ReadWrite);

        testBase64.DecodeOrignalObject("temp.xml", fileNew);

        //Compare the two files.
        if (testBase64.CompareResult( fileOld, fileNew)) {
            Console.WriteLine("The recreated binary file {0} is the same as
 {1}", args[1], args[0] );
        } else {
            Console.WriteLine("The recreated binary file {0} is not the same
 as {1}", args[1], args[0] );
        }

        fileOld.Flush();
        fileNew.Flush();
        fileOld.Close();
        fileNew.Close();

    }

    //Use the WriteBase64 method to create an XML document.  The object
  
    //passed by the user is encoded and included in the document.
    public void EncodeXmlFile(String xmlFileName,
 FileStream fileOld) {

        byte[] buffer = new byte[bufferSize];
        int readByte=0;

        XmlTextWriter xw = new XmlTextWriter(xmlFileName, Encoding.UTF8);
        xw.WriteStartDocument();
        xw.WriteStartElement("root");
        // Create a Char writer.
        BinaryReader br = new BinaryReader(fileOld);
        // Set the file pointer to the end.

        try {
              do {
                  readByte=br.Read(buffer, 0, bufferSize);
                  xw.WriteBase64(buffer, 0, readByte);
              } while (bufferSize <= readByte );

        } catch (Exception ex) {
            EndOfStreamException ex1= new EndOfStreamException();

            if (ex1.Equals(ex)) {
                Console.WriteLine("We are at end of file");
            } else {
                Console.WriteLine(ex);
            }
        }
        xw.WriteEndElement();
        xw.WriteEndDocument();

        xw.Flush();
        xw.Close();
    }

    //Use the ReadBase64 method to decode the new XML document 
    //and generate the original object.
    public void DecodeOrignalObject(String
 xmlFileName, FileStream fileNew) {

        byte[] buffer = new byte[bufferSize];
        int readByte=0;

        //Create a file to write the bmp back.
        BinaryWriter bw = new BinaryWriter(fileNew);

        XmlTextReader tr = new XmlTextReader(xmlFileName);
        tr.MoveToContent();
        Console.WriteLine(tr.Name);

        do {
          readByte=tr.ReadBase64(buffer, 0, bufferSize);
          bw.Write(buffer, 0, readByte);
        } while(readByte>=bufferSize);

        bw.Flush();

    }

    //Compare the two files.
    public bool CompareResult(FileStream fileOld,
 FileStream fileNew) {

        int readByteOld=0;
        int readByteNew=0;
        int count, readByte=0;

        byte[] bufferOld = new byte[bufferSize];
        byte[] bufferNew = new byte[bufferSize];


        BinaryReader binaryReaderOld = new BinaryReader(fileOld);
        BinaryReader binaryReaderNew = new BinaryReader(fileNew);

        binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin);
        binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin);


        do {
          readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize);
          readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize);

          if (readByteOld!=readByteNew) {
              return false;
          }

          for (count=0; count <bufferSize; ++count) {
              if (bufferOld[count]!=bufferNew[count]) {
                  return false;
              }
          }

        } while (count<readByte );

        return true;

    }

    //Display the usage statement.
    public void Usage() {
        Console.WriteLine("TestBase64 sourceFile, targetFile \n");
        Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp\n");
    }

}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Text;
ref class TestBase64
{
private:
   static int bufferSize = 4096;

public:

   // Use the WriteBase64 method to create an XML document.  The object
  
   // passed by the user is encoded and included in the document.
   void EncodeXmlFile( String^ xmlFileName, FileStream^ fileOld
 )
   {
      array<Byte>^buffer = gcnew array<Byte>(bufferSize);
      int readByte = 0;
      XmlTextWriter^ xw = gcnew XmlTextWriter( xmlFileName,Encoding::UTF8 );
      xw->WriteStartDocument();
      xw->WriteStartElement( "root" );

      // Create a Char writer.
      BinaryReader^ br = gcnew BinaryReader( fileOld );

      // Set the file pointer to the end.
      try
      {
         do
         {
            readByte = br->Read( buffer, 0, bufferSize );
            xw->WriteBase64( buffer, 0, readByte );
         }
         while ( bufferSize <= readByte );
      }
      catch ( Exception^ ex ) 
      {
         EndOfStreamException^ ex1 = gcnew EndOfStreamException;
         if ( ex1->Equals( ex ) )
                  Console::WriteLine( "We are at end of file" );
         else
                  Console::WriteLine( ex );
      }

      xw->WriteEndElement();
      xw->WriteEndDocument();
      xw->Flush();
      xw->Close();
   }

   // Use the ReadBase64 method to decode the new XML document 
   // and generate the original object.
   void DecodeOrignalObject( String^ xmlFileName, FileStream^
 fileNew )
   {
      array<Byte>^buffer = gcnew array<Byte>(bufferSize);
      int readByte = 0;

      // Create a file to write the bmp back.
      BinaryWriter^ bw = gcnew BinaryWriter( fileNew );
      XmlTextReader^ tr = gcnew XmlTextReader( xmlFileName );
      tr->MoveToContent();
      Console::WriteLine( tr->Name );
      do
      {
         readByte = tr->ReadBase64( buffer, 0, bufferSize );
         bw->Write( buffer, 0, readByte );
      }
      while ( readByte >= bufferSize );

      bw->Flush();
   }

   // Compare the two files.
   bool CompareResult( FileStream^ fileOld, FileStream^ fileNew
 )
   {
      int readByteOld = 0;
      int readByteNew = 0;
      int count;
      int readByte = 0;
      array<Byte>^bufferOld = gcnew array<Byte>(bufferSize);
      array<Byte>^bufferNew = gcnew array<Byte>(bufferSize);
      BinaryReader^ binaryReaderOld = gcnew BinaryReader( fileOld );
      BinaryReader^ binaryReaderNew = gcnew BinaryReader( fileNew );
      binaryReaderOld->BaseStream->Seek( 0, SeekOrigin::Begin );
      binaryReaderNew->BaseStream->Seek( 0, SeekOrigin::Begin );
      do
      {
         readByteOld = binaryReaderOld->Read( bufferOld, 0, bufferSize );
         readByteNew = binaryReaderNew->Read( bufferNew, 0, bufferSize );
         if ( readByteOld != readByteNew )
                  return false;

         for ( count = 0; count < bufferSize; ++count )
            if ( bufferOld[ count ] != bufferNew[ count ] )
                        return false;
      }
      while ( count < readByte );

      return true;
   }

   // Display the usage statement.
   void Usage()
   {
      Console::WriteLine( "TestBase64 sourceFile, targetFile \n" );
      Console::WriteLine( "For example: TestBase64 winlogon.bmp, target.bmp\n"
 );
   }
};

int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   TestBase64^ testBase64 = gcnew TestBase64;

   // Check that the usage is correct.
   if ( args->Length < 3 )
   {
      testBase64->Usage();
      return 1;
   }

   FileStream^ fileOld = gcnew FileStream( args[ 1 ],FileMode::OpenOrCreate,FileAccess::Read,FileShare::Read
 );
   testBase64->EncodeXmlFile( "temp.xml", fileOld );
   FileStream^ fileNew = gcnew FileStream( args[ 2 ],FileMode::Create,FileAccess::ReadWrite,FileShare::ReadWrite
 );
   testBase64->DecodeOrignalObject( "temp.xml", fileNew );

   // Compare the two files.
   if ( testBase64->CompareResult( fileOld, fileNew ) )
      Console::WriteLine( "The recreated binary file {0} is the same as {1}",
 args[ 2 ], args[ 1 ] );
   else
      Console::WriteLine( "The recreated binary file {0} is not the same as
 {1}", args[ 2 ], args[ 1 ] );

   fileOld->Flush();
   fileNew->Flush();
   fileOld->Close();
   fileNew->Close();
   return 0;
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Text.*;

class TestBase64
{
    private int bufferSize = 4096;

    public static void main(String[]
 args)
    {
        TestBase64 testBase64 = new TestBase64();
        //Check that the usage string is correct.
        if (args.get_Length() < 2) {
            testBase64.Usage();
            return;
        }

        FileStream fileOld = new FileStream(args[0], FileMode.OpenOrCreate
,
            FileAccess.Read, FileShare.Read);
        testBase64.EncodeXmlFile("temp.xml", fileOld);

        FileStream fileNew = new FileStream(args[1], FileMode.Create
,
            FileAccess.ReadWrite, FileShare.ReadWrite);

        testBase64.DecodeOrignalObject("temp.xml", fileNew);
        //Compare the two files.
        if (testBase64.CompareResult(fileOld, fileNew)) {
            Console.WriteLine("The recreated binary file {0} is the same as
 {1}",
                args[1], args[0]);
        }
        else {
            Console.WriteLine("The recreated binary file {0} is not the"
 
                + " same as {1}", args[1], args[0]);
        }

        fileOld.Flush();
        fileNew.Flush();
        fileOld.Close();
        fileNew.Close();
    } //main

    //Use the WriteBase64 method to create an XML document.  The object
  
    //passed by the user is encoded and included in the document.
    public void EncodeXmlFile(String xmlFileName,
 FileStream fileOld)
    {
        ubyte buffer[] = new ubyte[bufferSize];
        int readByte = 0;

        XmlTextWriter xw = new XmlTextWriter(xmlFileName, Encoding.get_UTF8());
        xw.WriteStartDocument();
        xw.WriteStartElement("root");
        // Create a Char writer.
        BinaryReader br = new BinaryReader(fileOld);
        // Set the file pointer to the end.
        try {
            do {
                readByte = br.Read(buffer, 0, bufferSize);
                xw.WriteBase64(buffer, 0, readByte);
            } while (bufferSize <= readByte);
        }
        catch (System.Exception ex) {
            EndOfStreamException ex1 = new EndOfStreamException();

            if (ex1.Equals(ex)) {
                Console.WriteLine("We are at end of file");
            }
            else {
                Console.WriteLine(ex);
            }
        }
        xw.WriteEndElement();
        xw.WriteEndDocument();

        xw.Flush();
        xw.Close();
    } //EncodeXmlFile

    //Use the ReadBase64 method to decode the new XML document 
    //and generate the original object.
    public void DecodeOrignalObject(String
 xmlFileName, FileStream fileNew)
    {
        ubyte buffer[] = new ubyte[bufferSize];
        int readByte = 0;
        //Create a file to write the bmp back.
        BinaryWriter bw = new BinaryWriter(fileNew);

        XmlTextReader tr = new XmlTextReader(xmlFileName);
        tr.MoveToContent();
        Console.WriteLine(tr.get_Name());

        do {
            readByte = tr.ReadBase64(buffer, 0, bufferSize);
            bw.Write(buffer, 0, readByte);
        } while (readByte >= bufferSize);

        bw.Flush();
    } //DecodeOrignalObject

    //Compare the two files.
    public boolean CompareResult(FileStream fileOld, FileStream
 fileNew)
    {
        int readByteOld = 0;
        int readByteNew = 0;
        int readByte = 0;
        int count;

        ubyte bufferOld[] = new ubyte[bufferSize];
        ubyte bufferNew[] = new ubyte[bufferSize];

        BinaryReader binaryReaderOld = new BinaryReader(fileOld);
        BinaryReader binaryReaderNew = new BinaryReader(fileNew);

        binaryReaderOld.get_BaseStream().Seek(0, SeekOrigin.Begin);
        binaryReaderNew.get_BaseStream().Seek(0, SeekOrigin.Begin);

        do {
            readByteOld = binaryReaderOld.Read(bufferOld, 0, bufferSize);
            readByteNew = binaryReaderNew.Read(bufferNew, 0, bufferSize);

            if (readByteOld != readByteNew) {
                return false;
            }

            for (count = 0; count < bufferSize; ++count) {
                if (bufferOld.get_Item(count) != bufferNew.get_Item(count))
 {
                    return false;
                }
            }
        } while (count < readByte);

        return true;
    } //CompareResult
    //Display the usage statement.
    
    public void Usage()
    {
        Console.WriteLine("TestBase64 sourceFile, targetFile \n");
        Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp\n");
    } //Usage
} //TestBase64 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からXmlTextWriter.WriteBase64 メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からXmlTextWriter.WriteBase64 メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からXmlTextWriter.WriteBase64 メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlTextWriter.WriteBase64 メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlTextWriter.WriteBase64 メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS