Append datas on xml file with xmlwriter class

Last post 05-16-2008 1:05 AM by Gery128. 6 replies.

Sort Posts:

  • Append datas on xml file with xmlwriter class

    05-15-2008, 8:12 AM
    • Loading...
    • esohier
    • Joined on 08-21-2002, 11:09 AM
    • Posts 16

    Hi,

    I've create a xml file using xmlwriter class.

    I've a problem to append new data on this file, because the create method of xmlwriter overwrite the xml file.

    How can I append datas with xmlwriter without lost previous datas?

    Thanks by advance.

    -- Eric
  • Re: Append datas on xml file with xmlwriter class

    05-15-2008, 8:44 AM
    Answer
    • Loading...
    • Gery128
    • Joined on 02-02-2008, 8:18 PM
    • Pune, India
    • Posts 48

    Hi Eshohier,

    You can use XMLDocument class to load XML file already existing on server. for that first add the namespace "using System.Data.Xml;"

    Then create a new instance of XMLDocument like , XMLDocument doc = new XMLDocumen(); Then just load xml by saying doc.Load("pass ur xml file path here"); and then you can create XMLNode and append it to the current xml. lastly you can save xml by saying doc.save("pass ur xml file path");

    if

     

    Regards,
    Girish Advani
    ---------------------------------------------------------------------------
    Remember to click “Mark as Answer” on the post that helps you.
    ---------------------------------------------------------------------------
  • Re: Append datas on xml file with xmlwriter class

    05-15-2008, 8:44 AM
    • Loading...
    • Gery128
    • Joined on 02-02-2008, 8:18 PM
    • Pune, India
    • Posts 48

    Hi Eshohier,

    You can use XMLDocument class to load XML file already existing on server. for that first add the namespace "using System.Data.Xml;"

    Then create a new instance of XMLDocument like , XMLDocument doc = new XMLDocumen(); Then just load xml by saying doc.Load("pass ur xml file path here"); and then you can create XMLNode and append it to the current xml. lastly you can save xml by saying doc.save("pass ur xml file path");

    if

    Regards,
    Girish Advani
    ---------------------------------------------------------------------------
    Remember to click “Mark as Answer” on the post that helps you.
    ---------------------------------------------------------------------------
  • Re: Append datas on xml file with xmlwriter class

    05-15-2008, 8:44 AM
    • Loading...
    • Gery128
    • Joined on 02-02-2008, 8:18 PM
    • Pune, India
    • Posts 48

    Hi Eshohier,

    You can use XMLDocument class to load XML file already existing on server. for that first add the namespace "using System.Data.Xml;"

    Then create a new instance of XMLDocument like , XMLDocument doc = new XMLDocumen(); Then just load xml by saying doc.Load("pass ur xml file path here"); and then you can create XMLNode and append it to the current xml. lastly you can save xml by saying doc.save("pass ur xml file path");

    if

    you
    Regards,
    Girish Advani
    ---------------------------------------------------------------------------
    Remember to click “Mark as Answer” on the post that helps you.
    ---------------------------------------------------------------------------
  • Re: Append datas on xml file with xmlwriter class

    05-15-2008, 8:50 AM
    • Loading...
    • esohier
    • Joined on 08-21-2002, 11:09 AM
    • Posts 16

    Hi and thanks for your answer.

    Can you give me a true example to do this? More explicit to create XML node and append it?

    How can I convert my xmlwriter class? 

    -- Eric
  • Re: Append datas on xml file with xmlwriter class

    05-15-2008, 10:17 AM
    Answer
    Hi Eric
    With .NET 2.0 there is another new overload of XmlWriter which takes in a TextWriter which is used to specify if you need to append to the file or not
    Here is a sample XmlWriterSettings settings = null;
    XmlWriter writer = null;

    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    settings.NewLineOnAttributes = true;

    settings = new XmlWriterSettings();

    using (StreamWriter fileWriter = new StreamWriter(@"filenamewithpath", true))
    {
    writer = XmlWriter.Create(fileWriter, settings);

    writer.WriteStartElement("Order");
    writer.WriteAttributeString("OrderID", txtOrderID.Text);
    writer.WriteAttributeString("Date", DateTime.Now.ToShortDateString());
    writer.WriteElementString("Amount", txtAmount.Text);
    writer.WriteEndElement();

    writer.Flush();
    }


    Girish's method would be required if you would like to insert the nodes at a particular tree hierarchy than just appending at the end.
    Happy programming
    Anton
  • Re: Append datas on xml file with xmlwriter class

    05-16-2008, 1:05 AM
    Answer
    • Loading...
    • Gery128
    • Joined on 02-02-2008, 8:18 PM
    • Pune, India
    • Posts 48

    there is no need to convert xmlWriter class. 

    string xmlFile = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Candidates.xml");

    xmldoc = new XmlDocument();

    xmldoc.Load(xmlFile);

    root = xmldoc.DocumentElement;

    try

    {

    XmlNode CandidateNode = xmldoc.CreateNode(XmlNodeType.Element, "Candidate", "");

    XmlNode id = xmldoc.CreateNode(XmlNodeType.Element, "CandidateId", "");

    id.InnerText = "1";

    CandidateNode.AppendChild(id);

    XmlNode subPositionId = xmldoc.CreateNode(XmlNodeType.Element, "SubPositionId", "");

    subPositionId.InnerText = candidate.PositionId.ToString();

    CandidateNode.AppendChild(subPositionId);

    XmlNode firstName = xmldoc.CreateNode(XmlNodeType.Element, "FirstName", "");

    firstName.InnerText = candidate.FirstName;

    XmlNode lastName = xmldoc.CreateNode(XmlNodeType.Element, "LastName", "");

    lastName.InnerText = candidate.LastName;

    CandidateNode.AppendChild(firstName);

    CandidateNode.AppendChild(lastName);

    root.AppendChild(CandidateNode);

    xmldoc.Save(xmlFile);

     

    This will help you.

    Regards,
    Girish Advani
    ---------------------------------------------------------------------------
    Remember to click “Mark as Answer” on the post that helps you.
    ---------------------------------------------------------------------------
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter