How i create a xml file
I want to know that i am using a two textboxes in one textbox i am entering the name and the other textbox i am entering the address.I take a button in my projects and when i will click on the button,i want a xml file which is automatically created and which the name and address in the xml format.How i will create it,please help me......
Suthish NairPosted Sep 21, 2011, 4:39 PM
Sam HobbsPosted Sep 21, 2011, 1:17 PM
Mamta MPosted Sep 21, 2011, 4:54 AM
Firstly there is no Sir here. There are many male and female members of this forum who can help you. So please dont write Hello Sir.
Secondly, your query is vague. What are present in your textboxes? In what form do you want to generate your xml? I mean, what are the elements, what are the attributes, are they both coming from the text boxes or only elements are being taken from text boxeS? You need to tackle these questions first. Then you can use the logic similar to below for writing:
public void WriteData()
{
// Store the XML file.
string fileName = "C:\\flights.xml";
// Create an XMLTextWriter object to write into the XML document, for eg flights.
XmlTextWriter Writer = new XmlTextWriter(fileName, null);
// Specify the formatting of the document.
Writer.Formatting = Formatting.Indented;
Writer.Indentation = 6;
// Write the XML declaration.
Writer.WriteStartDocument();
// Write the root node of the document.
Writer.WriteStartElement("", "FlightBooking", "");
// Write the tags needed.
Writer.WriteStartElement("", "BookingDetails", "");
Writer.WriteStartElement("", "From", "");
// Write the data inside the tag.
Writer.WriteString("London");
// Close the element.
Writer.WriteEndElement();
Writer.WriteStartElement("", "To", "");
Writer.WriteString("Paris");
Writer.WriteEndElement();
Writer.WriteStartElement("", "Departure", "");
Writer.WriteString("26/06/07");
Writer.WriteEndElement();
Writer.WriteStartElement("", "Return", "");
Writer.WriteString("26/09/07");
Writer.WriteEndElement();
Writer.WriteStartElement("", "Class", "");
Writer.WriteString("Economy");
Writer.WriteEndElement();
Writer.WriteEndElement();
Writer.WriteStartElement("", "PassengerDetails", "");
Writer.WriteStartElement("", "Adults", "");
Writer.WriteString("2 ");
Writer.WriteEndElement();
Writer.WriteStartElement("", "Children", "");
Writer.WriteString("Null");
Writer.WriteEndElement();
Writer.WriteStartElement("", "Infants", "");
Writer.WriteString("Null");
Writer.WriteEndElement();
Writer.WriteEndElement();
Writer.WriteStartElement("", "FlightsAvailable", "");
. . .
Writer.WriteEndElement();
// Flush the buffer and write data to the file.
Writer.Flush();
// Close the file.
Writer.Close();
}