Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I've used this code to read an existing xml file -

using (XmlReader reader = XmlReader.Create("XMLFile1.xml"))
{
  while (reader.Read())
  {
    switch (reader.NodeType)
    {
      case XmlNodeType.Element:
        if (reader.ReadToFollowing("title"))
        {
          output.AppendLine("Title: " + reader.ReadElementContentAsString());
          output.AppendLine();
        }
        if (reader.ReadToFollowing("first-name"))
        {
          output.AppendLine("First Name: " + reader.ReadElementContentAsString());
          output.AppendLine();
        }
        if (reader.ReadToFollowing("last-name"))
        {
          output.AppendLine("Last Name: " + reader.ReadElementContentAsString());
          output.AppendLine();
        }
        if (reader.ReadToFollowing("price"))
        {
          output.AppendLine("Price: " + reader.ReadElementContentAsString());
        }

        break;
    }
  }
  textblockTitle.Text = output.ToString();

  output.Remove(0, output.Length);
}



(where output is a StringBuilder object)

this is my XML file (XMLFile1.xml)


<!-- This is a sample XML document -->
<bookstore>
  <book genre="autobiography" publicationdate="1981-03-22" isbn="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
</bookstore>



Now i want to write/append to this file,
how could i do this?
(XMLFile1.xml is an existing xml file.)
Posted
Updated 19-Jan-11 1:41am
v3

See THIS[^] thread on appending data to XML. This might help you.
 
Share this answer
 
Comments
TweakBird 19-Jan-11 7:49am    
Hi Hiren. This is your 500th Answer. Keep rocking Man!
Hiren solanki 19-Jan-11 7:53am    
Thanks Eswar.
I was not aware of this.

Take a virtual five for this info as I coudn't do it with comments.
DeepsMann 24-Jan-11 2:25am    
thanks
Use the XmlDocument class instead.

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]

Good luck!
 
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