none
C# add element toexisting xml file

    Question

  • Hey guys i have an aplication that will log the the name,score,won/loss,ect. into an xml file witch looks lke this

    <log>

      <Game_1>

        <player>Name</player>

        <outcome>Won </outcome>

      </Game_1>

    </log>

     

    How can I reopen this file and add another game in between the log nodes?

    Tuesday, February 22, 2011 6:27 AM

Answers

  • Hello,

    Use the XmlDocument class to get access to your xml file. Also, please read this article: http://omegacoder.com/?p=46


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Proposed as answer by Neddy Ren Wednesday, February 23, 2011 9:09 AM
    • Marked as answer by Neddy Ren Monday, February 28, 2011 3:21 AM
    Tuesday, February 22, 2011 6:33 AM
  • Hi,

    Using System.XML.Linq;
    string xGame="<log> <Game_1>  <player>Name</player>  <outcome>Won </outcome> </Game_1></log>"
    XElement elem=XElement.Parse(xGame);
    XElement Additems=elem.Add(new XElement("log",new XElement("Game_2","Game2"),
    		new XElement("Player","Name"),
    		new XElement("outcome","Loss")));
    Additems.Save(@"C:\\Sample.xml");
    

    Nanda
    • Proposed as answer by Cornel Croitoriu Tuesday, February 22, 2011 6:51 AM
    • Marked as answer by Neddy Ren Monday, February 28, 2011 3:21 AM
    Tuesday, February 22, 2011 6:49 AM

All replies

  • Hello,

    Use the XmlDocument class to get access to your xml file. Also, please read this article: http://omegacoder.com/?p=46


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Proposed as answer by Neddy Ren Wednesday, February 23, 2011 9:09 AM
    • Marked as answer by Neddy Ren Monday, February 28, 2011 3:21 AM
    Tuesday, February 22, 2011 6:33 AM
  • Hi, Try this, Add using System.Xml & System.Xml.Xpath in the top string xml = @"<log> <Game_1> <player>Name</player> <outcome>Won </outcome> </Game_1> </log>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement gameElement =doc.CreateElement("Game_1"); XmlElement playerElement = doc.CreateElement("player"); playerElement.InnerXml = "Name2"; XmlElement outComeElement = doc.CreateElement("outcome"); outComeElement.InnerXml = "Loss"; gameElement.AppendChild(playerElement); gameElement.AppendChild(outComeElement); XmlNode node = doc.SelectSingleNode("//log"); node.AppendChild(gameElement); Save the doc file to local dirve using doc.Save(path);
    R.Mani http://rmanimaran.wordpress.com
    Tuesday, February 22, 2011 6:42 AM
  • Hi,

    Using System.XML.Linq;
    string xGame="<log> <Game_1>  <player>Name</player>  <outcome>Won </outcome> </Game_1></log>"
    XElement elem=XElement.Parse(xGame);
    XElement Additems=elem.Add(new XElement("log",new XElement("Game_2","Game2"),
    		new XElement("Player","Name"),
    		new XElement("outcome","Loss")));
    Additems.Save(@"C:\\Sample.xml");
    

    Nanda
    • Proposed as answer by Cornel Croitoriu Tuesday, February 22, 2011 6:51 AM
    • Marked as answer by Neddy Ren Monday, February 28, 2011 3:21 AM
    Tuesday, February 22, 2011 6:49 AM