Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write a code..

protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument myxmldocument = new XmlDocument();
        myxmldocument.Load(Server.MapPath("Register.xml"));
        XmlNode myxmlnode = myxmldocument.DocumentElement.FirstChild;

        XmlElement myXmlElement = myxmldocument.CreateElement("Name", Server.HtmlEncode(TextBox1.Text));
       
        myXmlElement.SetAttribute("Name", Server.HtmlEncode(TextBox1.Text));
        myXmlElement.SetAttribute("Password", Server.HtmlEncode(TextBox2.Text));
        myXmlElement.SetAttribute("Email", Server.HtmlEncode(TextBox3.Text));
        myXmlElement.SetAttribute("Gender", Server.HtmlEncode(DropDownList1.SelectedItem.Text));
        myXmlElement.SetAttribute("Location", Server.HtmlEncode(TextBox4.Text));
        myXmlElement.SetAttribute("DateofBirth", Server.HtmlEncode(TextBox5.Text));

        myxmldocument.DocumentElement.InsertBefore(myXmlElement, myxmlnode);
        myxmldocument.Save(Server.MapPath("Register.xml")); 

         BindData(); 
         lblmsg.Text = "Record Entered Successfully Inside XML File..."; 
         TextBox1.Text="";
         TextBox2.Text="";
         TextBox3.Text="";
         TextBox4.Text="";
         TextBox5.Text="";
    } 

    public void BindData()
    {
        XmlTextReader myXmlReader = new XmlTextReader(Server.MapPath("Register.xml")); 
        myXmlReader.Close(); 
    }

the data patients like this
<employee name="Ravi" password="ravi" email="ravi@gmail.com" gender="Male" location="Hyderabad" dateofbirth="26/06/1989" xmlns="Ravi">


but i want actual format of xml
<employee>
<name>Ravi
<password>ravi
.
.
.
.
.

like this can u help me
Posted
Updated 1-Mar-12 18:44pm
v2
Comments
Varun Sareen 2-Mar-12 0:45am    
Edit For: Added pre tag
Anuja Pawar Indore 2-Mar-12 2:00am    
Why repost. Use Improve question
http://www.codeproject.com/Questions/338556/Insert-Data-into-Xml-file

Assuming your root element employee exists on the XML file, there would be a problem on your code, since myxmlnode will always point to null if your root node does not have child elements at the moment. You can probably edit the way you assign the value like this.
C#
XmlNode myxmlnode = myxmldocument.DocumentElement;

And then, you can then add child nodes to your root node(employee) like this.
C#
XmlElement myXmlElement = myxmldocument.CreateElement("Name");
myXmlElement.InnerText = Server.HtmlEncode(TextBox1.Text);
myxmlnode.AppendChild(myXmlElement);
 
Share this answer
 
Comments
thatraja 2-Mar-12 2:17am    
Right, 5!
Is this working correctly i add childs like this

C#
XmlText text = myxmldocument.CreateTextNode(Server.HtmlEncode(TextBox1.Text)); myxmldocument.DocumentElement.AppendChild(myXmlElement);
myxmldocument.DocumentElement.LastChild.AppendChild(text);


But o/p shows correct format.....
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900