c# code 39 generator REA DING A ND WRITING XML DOCUME NTS in C#

Printing Code-39 in C# REA DING A ND WRITING XML DOCUME NTS

CHAPTER 3 REA DING A ND WRITING XML DOCUME NTS
Generate USS Code 39 In Visual C#
Using Barcode creator for Visual Studio .NET Control to generate, create Code 39 Extended image in VS .NET applications.
www.OnBarcode.com
Scan Code 39 Full ASCII In C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
XmlTextWriter writer = new XmlTextWriter(textBox3.Text, null); writer.WriteStartDocument(); writer.WriteComment("File exported on " + DateTime.Now); writer.WriteStartElement(textBox5.Text, "table", textBox4.Text); while (reader.Read()) { if (radioButton1.Checked) { writer.WriteStartElement(textBox5.Text, "row", textBox4.Text); for (int i = 0; i < reader.FieldCount; i++) { writer.WriteStartElement(textBox5.Text, reader.GetName(i), textBox4.Text); writer.WriteString(reader.GetValue(i).ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); } else { writer.WriteStartElement(textBox5.Text, "row", textBox4.Text); for (int i = 0; i < reader.FieldCount; i++) { writer.WriteAttributeString(textBox5.Text, reader.GetName(i), textBox4.Text, reader.GetValue(i).ToString()); } writer.WriteEndElement(); } } writer.WriteEndElement(); writer.Close(); reader.Close(); cnn.Close(); }
Barcode Maker In Visual C#
Using Barcode encoder for .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Drawing Code 39 Full ASCII In C#.NET
Using Barcode maker for .NET Control to generate, create ANSI/AIM Code 39 image in VS .NET applications.
www.OnBarcode.com
C HAPTE R 3 READING A ND WRITIN G XML DOC UMEN TS
Encoding UPC A In Visual C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create UPC-A Supplement 5 image in .NET framework applications.
www.OnBarcode.com
Matrix Barcode Generator In Visual C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create Matrix 2D Barcode image in .NET applications.
www.OnBarcode.com
Notice the lines marked in bold. The WriteStartElement() and WriteAttributeString() methods have an overload that accepts a prefix and namespace URI. If you create XML documents by using these overloads, your document should resemble Figure 3-15.
Linear Barcode Printer In C#
Using Barcode creator for .NET framework Control to generate, create Linear Barcode image in .NET applications.
www.OnBarcode.com
Paint Rationalized Codabar In C#
Using Barcode encoder for .NET framework Control to generate, create ABC Codabar image in .NET applications.
www.OnBarcode.com
Figure 3-15. XML document with namespaces and prefix added Notice how the namespace has been added to the <table> element. Also notice how each element now bears the prefix.
Code 39 Full ASCII Encoder In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create ANSI/AIM Code 39 image in ASP.NET applications.
www.OnBarcode.com
ANSI/AIM Code 39 Reader In C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Dealing with Nontextual Data
Barcode Recognizer In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Draw USS Code 128 In Visual Studio .NET
Using Barcode encoder for Reporting Service Control to generate, create ANSI/AIM Code 128 image in Reporting Service applications.
www.OnBarcode.com
Up until now, we have been using XmlReader and XmlWriter to read textual data. However, at times you may need to deal with nontextual data as well. For example, you may want to serialize image files or binary files as XML data in order to pass it over the Internet in a firewallfriendly way. Thankfully, both XmlTextReader and XmlTextWriter provide ways to handle such situations. To help you understand how XmlTextReader and XmlTextWriter can be used to work with nontextual data, we will develop an application like the one shown in Figure 3-16.
Barcode Printer In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Drawing UPC-A In .NET
Using Barcode printer for VS .NET Control to generate, create UPCA image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 3 REA DING A ND WRITING XML DOCUME NTS
Decode Barcode In VB.NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Make QR Code 2d Barcode In None
Using Barcode maker for Microsoft Excel Control to generate, create Denso QR Bar Code image in Excel applications.
www.OnBarcode.com
Figure 3-16. Dealing with nontextual data The application allows you to read any image file and serialize it as an XML document. You can specify the source image filename and the destination XML filename in the text boxes. After you save the image as an XML document, you can validate whether the serialized image is correct by loading it in a picture box.
Paint UPC Symbol In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create UPC Code image in VS .NET applications.
www.OnBarcode.com
Code 3 Of 9 Printer In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications.
www.OnBarcode.com
Serializing Data
GTIN - 13 Recognizer In VB.NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
QR Creation In Java
Using Barcode creator for Android Control to generate, create QR Code ISO/IEC18004 image in Android applications.
www.OnBarcode.com
Listing 3-9 shows the Click event handler of the Save Image as XML button. Listing 3-9. Writing Base64 Data private void button1_Click(object sender, EventArgs e) { XmlTextWriter writer = new XmlTextWriter(textBox2.Text, null); FileStream fs = File.OpenRead(textBox1.Text); byte[] data = new byte[fs.Length]; fs.Position = 0; fs.Read(data, 0, data.Length); fs.Close(); writer.WriteStartDocument(); writer.WriteStartElement("imagefile"); writer.WriteAttributeString("filename", textBox1.Text); writer.WriteAttributeString("size", data.Length.ToString()); writer.WriteBase64(data,0,data.Length); writer.WriteEndElement(); writer.Close(); }
C HAPTE R 3 READING A ND WRITIN G XML DOC UMEN TS
The code creates an XmlTextWriter object by passing the path of the destination XML file to the constructor. Then a FileStream is created for reading data from the image file. The contents of the file are read by using the Read() method of the FileStream class, which accepts three parameters: the byte array to read the data into, the start index in the byte array from where the writing should start, and the length of data to read. The XmlTextWriter then starts writing the document. It first writes the XML processing instruction and the <imagefile> element. The <imagefile> element has two attributes: filename and size. The filename attribute stores the complete path of the image file that is being serialized as XML. The size attribute contains the size of the source image file. Image files contain nontextual data. You have a couple of options when you want to serialize nontextual data into XML files. You can use either hexadecimal encoding or Base64 encoding for the serialization. In our example, we use Base64 encoding. To write data into Base64 format, the XmlTextWriter class provides a method called WriteBase64(), which accepts three parameters: a byte array that contains the nontextual data, the index of the byte array from which the writing should start, and the length of data to write. The WriteBase64() method writes the supplied byte array as a Base64 string inside the destination XML element. Figure 3-17 shows how the XML file looks after serializing an image file.
Figure 3-17. Image file serialized in Base64 format Now that you know how to write nontextual data by using XmlTextWriter, you re ready to see how to use XmlTextReader to read the document back.
Copyright © OnBarcode.com . All rights reserved.