XML is very versatile and most commonly used data format in any applications we develop. Hence, there will be always a need to read and write xml files in our day today development tasks. Moving forward, this little article will help us understand how to read and write xml files using C in ASP. Netframework has packed with a class called XmlTextWriter in System.
XML namespace to create xml files dynamically. Next section will help us to understand the same. The XmlTextWriter class includes number of methods to create xml document content very easily. Consider we need to create a xml file called Employee. If you see the above xml content, you can visualize it as an xml document having xml elements Employees, Employee or nodes with attributes Type and values Satheesh. Keeping this in mind, to create the above XML document programmatically using XmlTextWriter class we need to first call WriteStartDocument to start the document creation which emits the xml declaration.
Remember, any xml nodes created should have the matching closing nodes. UTF8 ;. Once executed, it will create an xml document called Employee. You need to include the System. Xml and System. Text namespace for the above code to work. In the above example, we have created a very basic xml document using XmlTextWriter class.
This class has many more useful methods which can be used. Download the code attached to see it in action. WriteAttributeString "type", dt. ToString ;. WriteElementString "FirstName", dt. WriteElementString "LastName", dt. WriteElementString "Dept", dt. Like XmlTextWriter class, the System. Xml namespace includes XmlTextReader class which can be used to read xml document. AppendChild name ;.
AppendChild Qualification ;. AppendChild parentelement ;. Save Server. Step 7: Run your application by pressing F5. The output is:. Step 8 : See your sample. View All. Richa Garg Updated date May 02, If we use a XML file to store the data then we can do operations with the XML file directly without using the database. The XML format is supported for all applications.
It is independent of all software applications and it is accessible by all applications. For this the following procedure can be used.
Data; using System. Xml; Step 4: Now write the following code in the code behind file: public partial class Default2 : System. ReadXml xmlreader ; xmlreader. The two primary classes we will use in our examples and in this article are XmlWriter and XmlTextReader respectively. The XmlWriter class is a base class that provides a forward-only, read-only process to generate XML stream. Let me explain.
It provides methods to write data in an XML document. Good we know it, since in our example, we are using various methods of this class to write data and it is simple. Again, this class has some very nice properties and methods. I'll not discuss all the methods in this article, but if you find any interesting method, that you think I should have included in this article, then please let me know.
Let's see these classes and its methods in action. Open Visual Studio, from the file menu select new website , and choose the language of your choice. You can process these functions either on a button click event or on the page load event.
In the code behind section, add the below code to write data in an XML file. In our form load event, we have place the code to write data. The create method will create a new instance of XmlWrite class. It takes a parameter in the form of an outputFileName. Immediately after creating an instance, we will declare a method called WriteStartDocument.
This method opens data writing process which is later closed by WriteEndDocument. You must declare these two methods to start the entire process of writing in an XML file.
There are numerous other methods that we are not using, since it beyond the scope of this article. We may define each of the methods in other articles as well. In our example we have two tags , the first tag is the Library , followed the second tag List. It is very important to remember that these tags are case sensitive , and must declare and used carefully.
We have created the instance, declared start and end documents, followed by creating the two tags. Now we add actual data into the file. This is done using the WriteElementString method. This method takes two parameters, as you can see. The first parameter is the local name or simply the name of the element and second parameter is the value of the element. Few interesting and useful methods which we have not included in our example, but worth describing.
0コメント