ARTICLE

Create XML file using ASP.Net

Posted by Vamshi Krishna Articles | ASP.NET Programming March 29, 2011
How to create XML file using ASP.Net and store in solution explorer.
Reader Level:
Download Files:
 

Prepare Asp.net Web form:

XML.gif

XmlTextWriter Class:

.NET contains a number of classes that support XML. Many of these classes make working with XML as easy as understanding XML. I'm going to show you an example of one such class here. This is the XmlTextWriter class.

The XmlTextWriter class allows you to write XML to a file. This class contains a number of methods and properties that will do a lot of the work for you. To use this class, you create a new XmlTextWriter object.

WriteStartDocument: Writes the XML declaration with the version "1.0".

WriteEndDocument: Closes any open elements or attributes.

Close: Closes the stream.

WriteStartElement: Writes the specified start tag.

WriteEndElement: Closes one element.

WriteStartAttribute: Writes the start of an attribute.

WriteEndAttribute: Closes the previous WriteStartAttribute call.

WriteString: Writes a string.

Code Under Add Button:

protected void btnAdd_Click(object sender, EventArgs e)
    {
        book_name = Convert.ToString(txtBookName.Text);
        book_price = Convert.ToString(txtBookPrice.Text);
        author1_fname = Convert.ToString(txtAuthor1FName.Text);
        author1_lname = Convert.ToString(txtAuthor1LName.Text);
        author2_fname = Convert.ToString(txtAuthor2FName.Text);
        author2_lname = Convert.ToString(txtAuthor2LName.Text);
        author1_country = Convert.ToString(txtcountry1.Text);
        author1_state = Convert.ToString(txtstate1.Text);
        author2_country = Convert.ToString(txtcountry2.Text);
        author2_state = Convert.ToString(txtstate2.Text);
        xmlfile_name = Convert.ToString(txtFileName.Text);

        //Call Create

        create();
    }

    public void create()
    {
        //Start writer
        XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/XMLFiles/" + xmlfile_name + ".xml"), System.Text.Encoding.UTF8);

        //Start XM DOcument
        writer.WriteStartDocument(true);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 2;

        //ROOT Element
        writer.WriteStartElement("Books");

        //call create nodes method
        createNode(book_name, book_price, author1_fname, author1_lname, author2_fname, author2_lname, author1_country, author1_state, author2_country, author2_state, writer);

        writer.WriteEndElement();

        //End XML Document
        writer.WriteEndDocument();

        //Close writer
        writer.Close();
    }

private void createNode(string bName, string bPrice, string a1Fname,
     string a1LName, string a2Fname, string a2Lname,
     string a1Country,string a1State, string a2Country,
     string a2State, XmlTextWriter writer)
     {
        //parent node start
        writer.WriteStartElement(bName + "book");

        //Book name
        writer.WriteStartElement("Book_name");
        writer.WriteString(bName);
        writer.WriteEndElement();

        //Book Price
        writer.WriteStartElement("Book_price");
        writer.WriteString(bPrice);
        writer.WriteEndElement();

        //Start Authors
        writer.WriteStartElement("Authors");

        //Start Author1
        writer.WriteStartElement("Author1");

        //Start Author1 Name
        writer.WriteStartElement("Author1_name");

        //Author1 first Name
        writer.WriteStartElement("First_name");
        writer.WriteString(a1Fname);
        writer.WriteEndElement();

        //Author1 Last name
        writer.WriteStartElement("Last_name");
        writer.WriteString(a1LName);
        writer.WriteEndElement();

        //End Aauthor1 Name
        writer.WriteEndElement();

        //Start Author1 Address
        writer.WriteStartElement("Author1_Address");

        //Author1 Country  
        writer.WriteStartElement("Country");
        writer.WriteString(a1Country);
        writer.WriteEndElement();

        //Author1 State
        writer.WriteStartElement("State");
        writer.WriteString(a1State);
        writer.WriteEndElement();

        //End Author1Address
        writer.WriteEndElement();

        //End Author1
        writer.WriteEndElement();

        //Start Author2
        writer.WriteStartElement("Author2");

        //Start Author2 Name
        writer.WriteStartElement("Author2_name");

        //Author2 first Name
        writer.WriteStartElement("First_name");
        writer.WriteString(a2Fname);
        writer.WriteEndElement();

        //Author1 Last name
        writer.WriteStartElement("Last_name");
        writer.WriteString(a2Lname);
        writer.WriteEndElement();

        //End Aauthor1 Name
        writer.WriteEndElement();

        //Start Author2 Address
        writer.WriteStartElement("Author2_Address");

        //Author2 Country  
        writer.WriteStartElement("Country");
        writer.WriteString(a2Country);
        writer.WriteEndElement();

        //Author2 State
        writer.WriteStartElement("State");
        writer.WriteString(a2State);
        writer.WriteEndElement();

        //End Author2Address
        writer.WriteEndElement();

        //End Author2
        writer.WriteEndElement();

        //End Authors
        writer.WriteEndElement();
        lblResult.Text = "XML File ceated!";

     }
 

COMMENT USING

Trending up