Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<make name="BMW">
    <model name="3 Series">
      <color name="Red" />
      <color name="Black" />
      <color name="Blue" />
    </model>
</make>


I want to insert record in this above format in xml file using c#;
can anyone suggest me some way through which i can create sub node or tree format data in xml.

Thanks in advance
Posted

Refer this link
http://www.dotnetperls.com/xmlwriter
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/ee16f840-c983-40f0-a0b6-e664bf4b38fe

Try this code
C#
using (XmlWriter writer = XmlWriter.Create(Server.MapPath("BMW.xml")))
            {
                string[] Color = new string[3] { "Red", "Black", "Blue" };
                writer.WriteStartDocument();
                writer.WriteStartElement("make");
                writer.WriteAttributeString("name", "BMW");               
                writer.WriteStartElement("model");
                writer.WriteAttributeString("name", "3 Series");                

                for (int i = 0; i < Color.Length; i++)
                {
                    writer.WriteStartElement("Color");
                    writer.WriteAttributeString("name", Color[i].ToString());                    
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
 
Share this answer
 
v2
Comments
sahabiswarup 12-Sep-12 0:34am    
Thanks pradiprenushe
sahabiswarup 12-Sep-12 0:48am    
This above code works only once; if i run this code xml file is only overwritten with same data. what i have to do if i want to update the xml file so that records are generating continuously?
pradiprenushe 12-Sep-12 1:10am    
Go through this link
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/e3f3c6b1-43ee-46d7-bc09-edb8dcedb8d1
sahabiswarup 12-Sep-12 1:16am    
[:)]
Thanks a lot
pradiprenushe 12-Sep-12 1:21am    
Welcome
 
Share this answer
 

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