C# Archives - Page 2 of 2 - Coding Sharp

Tag: C#

Writing XML with the XmlWriter class

Writing XML with the XmlWriter class

Asp.Net, C#.Net
In previous article we have see about writing and reading text document, but now it's time to start dealing with XML file. Anyhow XML is just a plain text, we can simply start writing an XML tags to the file and save it with .xml file extension. But for .net framework it is very easy and safe to handle. For writing an XML file .net framework provides two different options. XMLWriter and XmlDocument, we will see XMLDocument in the next article. In this article we will focus on XMLWriter approach Let's see the basic different between XmlWriter and XmlDocument. XMLWriter uses less memory compare to XmlDocument, that is the only 1 issue if we are writing a vey big file. Another difference is if we are modofing an xml file using XmlDocument, we can read an exisitng file, change it as req...
StreamReader

StreamReader

Asp.Net, C#.Net
StreamReader class In my previous article we have have seen how to read from text file using File class and how to write into text file using StreamWriter class. In this article we will see how to read from text file using StreamReader  class. Using Statements: It is recommended to use using statements while we are using StreamReader class, which ensures that is is removed from the memory once it is no longer needed. The syntax is very easy to use. StreamReader class reads text file, it is available in System.IO namespace. It provides very good performance. Using StreamReader we can read a file synchronously and asynchronously, we need to provide a relative or absolute path to the file. Lets check with the example that how to synchronously read a line from text file using StreamR...
Understanding StreamWriter class

Understanding StreamWriter class

Asp.Net, C#.Net
We have seen how to write in text file using File class in our previous article. Today we will see how to use StreamWriter class to write in a text file. This class is very helpful to write text data into files. This class contains method to write to a file synchronously like Write and WriteLine or asynchronously WriteAsync and WriteLineAsync. Using Statements. It is recommended to use using statements while we are using Stream Writer class, which ensures that is is removed from the memory once it is no longer needed. The syntax is very easy to use. Lets check with the example that how to synchronously write to a text file using Stream Writer class. First we need to declare and initialize an instance of the class with using statement. It is available is available in System.IO namespace...
How to Read from a Text File using C#

How to Read from a Text File using C#

Asp.Net, C#.Net
In previous article we have see "How to write to a Text File using C#", Now in this article we are going to learn how can we read text file using C#. In most of the application either we are writing something to text file or reading from text file. Reading from and writing to text file is essential for major application. So lets see the code for reading text file using C#. The main important class is File for Read from text file which exists in System.IO namespace. So first we need to import System.IO namespace to our project. Now lets see example for ReadAllText method ReadAllText ReadAllText method will read all the content of a given file. It will take 1 parameter as a full file path. Example #1 ReadAllLines ReadAllLines method will read all the lines of a give...
How to write to a Text File using C#

How to write to a Text File using C#

Asp.Net, C#.Net
In this article we will see how to write text in txt file using C#. In our dot-net application frequently we required to either read from text file or write to text file. We will see how to read from txt file later. Here we will focus on writing into text file. The main class file we are going to user for either reading or writing is "System.IO.File" class. File class contains a various methods to write in a txt file. To write all the text in a file see the below example Example For WriteAllText Now you can also write the list of lines into a text file. See below example for more detail and syntax. Example For WriteAllLines Above both example code are creating a new file or over right an existing file. Now lets see how to append some text to an existing fi...
Introduction to Class in OOPS with C#.net

Introduction to Class in OOPS with C#.net

C#.Net, Special Posts
Understanding class A class is like a blueprint. It allows us to create our own custom data types by grouping methods, events and variables of different types like int, string or any other custom types. Class defines the data and behavior of a type. A class can be defined with a keyword "class" followed by the class name and body of the class with a pair of curly braces. Let's have a look at the general form of a class body. below is the general form of a class body. Classes supports inheritance while structs can't support it. Access specifiers plays an important role in the class definition, which specify the rules for accessing the member as well as the class itself. Default access specifier for the class is internal while for member it is private. Data type specifies ...