Académique Documents
Professionnel Documents
Culture Documents
File Input/Output
Introduction to XML
Data Representation through XML Working with XMLReader and XMLWriter
System.IO Namespace
A data file is a computer file that can be
processed, manipulated or used as input by a computer program. Computer programs also create data files as output. A data file is considered a named collection of bytes having persistent or lasting storage. When working with data files, you think in terms of directory paths, disk storage, and files and directory names. The .net framework includes the System.IO namespace, which provides basic file and directory support classes. It also contains types that enable you to read and write files and data streams.
System.IO Classes
Class Binary Reader and Writer Directory, File, DirectoryInfo, and FileInfo FileStream MemoryStream StreamWriter and StreamReader Description Read and write primitive data types Create, delete, and move files and directories. Get specific information about the files by making use of the properties defined in these classes Access the files in a random fashion Access data stored in memory Read and write textual information
GetLastWriteTime Gets the date and time the specified file was last () written to
GetCurrentDirector Gets the current working directory of the application y() GetDirectories() Gets the names of sub-directories in a specified directory
GetFiles()
GetLastWriteTime( ) Move()
StreamWriter Members
Name Close() Dispose() Flush() Description Closes the current StreamWriter Releases the unmanaged resources used by the StreamWriter Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream Gets or Sets the line terminator string used by the current TextWriter Writes the characters to the stream Writes the characters to the stream, followed by a line terminator
StreamReader Members
Name Close() Dispose() Peek() Read() ReadBlock() Description Closes the current StreamReader Releases the unmanaged resources used by the StreamReader Returns the next available character but does not consume it Reads the next character or next set of characters from the input stream Reads a specified number of characters from the current stream and writes the data to buffer, beginning at the index Reads a line of characters from the current stream and returns the data as a string Reads the stream from the current position to the end of the stream
ReadLine() ReadToEnd()
Create File
Read File
RichTextBox Control
private void button1_Click(object sender, EventArgs e) { FileInfo f = new FileInfo("D:/Mytext.txt"); StreamWriter w = f.CreateText(); w.WriteLine("This is from"); w.WriteLine("Chapter 6"); w.WriteLine("Of C# Module"); w.Write(w.NewLine); w.WriteLine("Thanks for your time"); w.Close(); MessageBox.Show("File Created Successfully"); } private void button2_Click(object sender, EventArgs e) { StreamReader s = File.OpenText("D:/Mytext.txt"); string read = null; while ((read = s.ReadLine()) != null) { richTextBox1.AppendText(read); } s.Close(); }
BinaryWriter Members
Name Description
Close()
Flush()
Seek()
Write()
BinaryReader Members
Name Close() PeekChar() Read() Description Closes the current BinaryReader and the underlying stream Returns the next available character and does not advance the byte or character position Reads characters from the underlying stream and advances the current position of the stream
ReadBoolean Reads a boolean value from the current stream and () advances the current position of the stream by 1 byte ReadByte() ReadChar() ReadInt32() Reads the next byte from the current stream and advances the current position of the stream by 1 byte Reads the next character from the current stream and advances the current position of the stream Reads a 4-byte signed integer from the current stream and advances the current position of the stream by 4 bytes
Introduction to XML
Introduction to XML
XML is one of the most important component in
the .NET platform. Recognizing the simplicity and power of XML in representing and transmitting data, it have been effectively harnessed in the .NET platform. The XML API in .NET is extensive and provides a wide array of types(classes) to manipulate with XML and related technologies very easily, effectively and with a significant gain in performance.
Introduction to XML
XML stands for Extensible Markup Language.
XML files store data in a structured format by using meaningful tags. XML stores structured data in XML documents that are similar to databases. Notice that unlike Databases, XML documents store data in the form of plain text, which can be used across platforms. Following are the benefits of storing data in XML files: XML files enable the fast searching of records XML file stores data in a user defined format XML files stores data in hierarchically nested elements
of the data by creating a DTD or an XML schema. When you include a DTD in an XML document, the software checks the structure of the XML document against the DTD. This process of checking the structure of the XML document is called validating. The software performing the task of validating is called a validating parser.
The
DataSet class provides a WriteXml method to write a dataset data to an XML file.
database to XML document: 1. Create the connection with the database 2. Create a data adapter object and select all the records of the table. 3. Now using the fill method fill the DataSet from the data adapter 4. Use the WriteXml method of dataset to create the XML document and write its content to the XML document.
document that will store data related to books: <?xml version="1.0"?> <Books> <Book bid="B001"> <Title> Understanding XML </Title> <Price> $30 </Price> <author> <FirstName> Lily </FirstName> <LastName>Hicks <LastName> </author> </Book>
<Title> .NET Framework </Title> <Price> $45 </Price> <author> <FirstName> Jasmine </FirstName> <LastName> Williams <LastName> </author> </Book> </Books>
displayed using XmlTextReader. The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReaderClass. Besides XmlReader methods and properties, these classes also contain members to read text, node, and schemas respectively. After creating an instance of XmlTextReader, call Read method to start reading the document. After read method is called, you can read all information and data stored in a document. XmlReader class has properties such as Name,
Using XmlReader
Using System; Using System.Xml; namespace ReadXml1 { class Class1 { public static void Main(string[] args) { XmlTextReader textReader=new XmlTextReader(C:\\books.xml); textReader.Read(); while(textReader.Read()) { textReader.MoveToElement(); Console.WriteLine(XmlTextReader Properties Test); Console.WriteLine(=================);
Using XmlReader
Console.WriteLine(Name: + textReader.Name); Console.WriteLine(Base URI: + textReader.BaseURI); Console.WriteLine(Local Name: + textReader.LocalName); Console.WriteLine(Attribute Count: + textReader.AttributeCount.ToString()); Console.WriteLine(Depth: + textReader.Depth.ToString()); Console.WriteLine(Line Number: + textReader.LineNumber.ToString()); Console.WriteLine(Node Type: + textReader.NodeType.ToString()); Console.WriteLine(Attribute Count: +
textReader.Value.ToString());
write to XML documents. It is an abstract base class used through XmlTextWriter and XmlNodeWriter classes. It contains properties and methods to write to XML documents. This class includes WriteNode, WriteString, WriteAttributes, WriteStartElement, and WriteEndElement methods to write every type of item of an XML document. XmlTextWriter has three overloaded constructors, which can take a string, stream, or a TextWriter as an argument.
Using XmlWriter
Using System; Using System.Xml; namespace ReadingXML2 { class Class1 { public static void Main(String args[]) {
XmlTextWriter textwriter=new
XmlTextWriter(C:\\myXmlFile.xml,null); textWriter.WriteStartDocument(); textWriter.WriteComment(First Comment XmlTextWriter Sample Example); textWriter.WriteComment(myXmlFile.xml in root dir); textWriter.WriteStartElement(Student); textWriter.WriteStartElement(r, RECORD, urn:record);
Using XmlWriter
textWriter.WriteStartElement(Name, ); textWriter.WriteString(Student); textWriter.WriteEndElement(); textWriter.WriteStartElement(Address, ); textWriter.WriteString(Colony); textWriter.WriteEndElement(); char[] ch=new char[3]; ch[0]=a; ch[1]=r; ch[2]=c; textWriter.WriteStartElement(Char); textWriter.WriteChars(ch,0,ch.Length); textWriter.WriteEndElement(); textWriter.WriteEndDocument(); textWriter.Close(); }}}