asp. net stores an image as a binary value in the instance code in the Xml file

  • 2020-06-15 08:07:15
  • OfStack


try
   {
    int readByte = 0;        //
    int bytesToRead = 100;       // Data buffer size 
    string fileName = "../../WriteXml.xml";   // The file to open 
    //   this.textBox1.Text = string.Empty;            
    //  Open the image file and use the image to construct 1 A file stream 
    FileStream fs = new FileStream("../../001.jpg",FileMode.Open);
    //  Use file stream construction 1 a 2 The base reader reads the base metadata as 2 Hexadecimal values 
    BinaryReader br = new BinaryReader(fs);
    XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8);
    // Output Settings   Code indentation 
    xmlTxtWt.Formatting = Formatting.Indented;
    //   xmlTxtWt.Indentation = 4;
    // Write a statement 
    xmlTxtWt.WriteStartDocument();
    xmlTxtWt.WriteStartElement("picture","ContactDetails","https://www.ofstack.com");// Define a namespace 
    xmlTxtWt.WriteStartElement("image");            // Define the node 
    xmlTxtWt.WriteAttributeString("imageName","002.jpg");        // Add image attributes 
    byte[] base64buffer = new byte[bytesToRead];          // Open buffer 
    do
    {
     readByte = br.Read(base64buffer,0,bytesToRead);      // Reads the data into a byte array  
     xmlTxtWt.WriteBase64(base64buffer,0,readByte);       // Will be in the array 2 The base value is encoded as Base64 And write to XML file 
    }while(bytesToRead <= readByte);
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndDocument();
//    xmlTxtWt.Flush();
    xmlTxtWt.Close();
    MessageBox.Show(" Read-write finished! ");
    //   this.textBox1.Text = ReadXml(fileName);
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.ToString());
   }

Related articles: