how to create write and read xml file in console application
Step 1:
-> Open vs
->Go to file
->New ->project -> choose Console Application
-> Give Any name and Click Add.
Step2:
-> Go to Solution Explorer
-> Right Click Project Name
->Go to Add
-> New Item
-> Select xml file
->Give Any Name(Here:Products.xml)
->Ok
Step3:
->Add Namespace
using System.Xml;
Step4:
-> Write code for create write and read
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace xmlwrite
Program
{
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("Products.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("Product");
writer.WriteAttributeString("ID", "001");
writer.WriteAttributeString("Name", "Laptop");
writer.WriteElementString("Price", "30000.00");
writer.WriteStartElement("OtherDetails");
writer.WriteElementString("BrandName", "Dell");
writer.WriteElementString("Manufacturer", "DellCompny");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush(); writer.Close();
XmlTextReader reader = new XmlTextReader("Products.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element. Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element. Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
}
}
Note:-
->We use the XmlWriterSettings class to create a setting for the XmlWriter that we will use.
-> We then create an XmlWriter object by calling the XmlWriter.Create method and supplying the filename and the XmlWriterSettings object.
-> We Use WriteStartElement() supply the name of the element as the argument.
->We Use WriteStartElement() writes the closing tag of that element..
->We use WriteAttributeString() method and supply the name and the value for that attribute.
-> We use .WriteElementString() method with a name and a value as the arguments to write an element that is wrapping a value.
->.WriteStartDocument() method which writes the xml declaration
-> we used the Writer.Flush() method to clean the contents of the stream.
-> XmlWriter.Close() method to save the file and stop the program from using it.
Step5:
->Run the program
and see output
Step 1:
-> Open vs
->Go to file
->New ->project -> choose Console Application
-> Give Any name and Click Add.
Step2:
-> Go to Solution Explorer
-> Right Click Project Name
->Go to Add
-> New Item
-> Select xml file
->Give Any Name(Here:Products.xml)
->Ok
Step3:
->Add Namespace
using System.Xml;
Step4:
-> Write code for create write and read
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace xmlwrite
Program
{
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("Products.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("Product");
writer.WriteAttributeString("ID", "001");
writer.WriteAttributeString("Name", "Laptop");
writer.WriteElementString("Price", "30000.00");
writer.WriteStartElement("OtherDetails");
writer.WriteElementString("BrandName", "Dell");
writer.WriteElementString("Manufacturer", "DellCompny");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush(); writer.Close();
XmlTextReader reader = new XmlTextReader("Products.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element. Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element. Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element. Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
}
}
Note:-
->We use the XmlWriterSettings class to create a setting for the XmlWriter that we will use.
-> We then create an XmlWriter object by calling the XmlWriter.Create method and supplying the filename and the XmlWriterSettings object.
-> We Use WriteStartElement() supply the name of the element as the argument.
->We Use WriteStartElement() writes the closing tag of that element..
->We use WriteAttributeString() method and supply the name and the value for that attribute.
-> We use .WriteElementString() method with a name and a value as the arguments to write an element that is wrapping a value.
->.WriteStartDocument() method which writes the xml declaration
-> we used the Writer.Flush() method to clean the contents of the stream.
-> XmlWriter.Close() method to save the file and stop the program from using it.
Step5:
->Run the program
and see output
No comments:
Post a Comment