Add multiple records to xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganesh22
    Banned
    New Member
    • Sep 2007
    • 81

    Add multiple records to xml file

    Hi,
    Code:
            xmlw = new XmlTextWriter("c:\\testing.xml",null);
                xmlw.WriteStartDocument();
                xmlw.WriteStartElement("Company");
                xmlw.WriteStartElement("Software");
                xmlw.WriteStartElement("Skills");
                xmlw.WriteElementString("Microsoft", textBox1.Text);
                xmlw.WriteElementString("Sun", textBox2.Text);
                xmlw.WriteEndElement();
                xmlw.WriteStartElement("Management");
                xmlw.WriteElementString("Admin", textBox3.Text);
                xmlw.WriteElementString("Assist", textBox4.Text);            
                xmlw.WriteEndElement();
                xmlw.WriteEndElement();
                xmlw.WriteEndElement();
                xmlw.WriteEndDocument();
                xmlw.Close();
    The above code is for creating an xml, its working fine now iam having two requirements based on above code
    1)after writing (xmlw.Close()) xml i want to display that xml in text box
    2) I want to add multiple records in that xmlfile bcoz it creatinng only one record then we added another record means its replacing the previous record
    I want to add multiple records in xml how?
    Last edited by acoder; Jul 20 '08, 10:11 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    As a full member now, you should know that we expect your code to be posted in [CODE] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use the tags in future.

    MODERATOR.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by ganesh22
      The above code is for creating an xml, its working fine now iam having two requirements based on above code
      1)after writing (xmlw.Close()) xml i want to display that xml in text box
      2) I want to add multiple records in that xmlfile bcoz it creatinng only one record then we added another record means its replacing the previous record
      I want to add multiple records in xml how?
      This is not an XML question really. This will be better answered in a language-specific forum (.NET I presume).

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        1. Questions:
        Do you really need to save the xml in a file, as opposed to some runtime memory object, perhaps as a string even?

        If so, you could onClose, create a function which reads the text from the xml file and puts it into the textBox. However this seems like more work than necessary. It'd be easier if you had your XmlTextWriter write to a stringBuffer or stream, and then output the string to both the xml file and the textBox.

        2. You need to create a function to read the xml you've written, eg an XmlReader.
        After reading the xml in, you'd have a loop that rewrites the data to the new result as it reads it in from the old. Event style input/output is definitely the way to go, (eg the way you already have it by using XmlTextWriter). DOM would be overkill.

        Comment

        Working...