Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. New to XML

New to XML

Scheduled Pinned Locked Moved XML / XSL
designsysadminxmlhelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lehya
    wrote on last edited by
    #1

    I have a page with data retrieving from user: Name, landline no, Question to ask and Timings..Once i click on button of this page, say callback.aspx, it will generate a xml file automatically with a file name userinfo.xml Now i try to read this file in another .aspx page say read.aspx from userinfo.xml...Everything is working fine..except for.. If another user enters info in callback and submits. And when i check it from read.aspx or userinfo.xml the previous data is lost and the new data is written...Is there any possibility that dynamically data can be appended to userinfo.xml Please help..the following is the code in callback.aspx string xmlFile = Server.MapPath("userInfo.xml"); XmlTextWriter writer = new XmlTextWriter(xmlFile, Encoding.UTF8); //start writing writer.WriteStartDocument(); writer.WriteComment("Posted on @" + DateTime.Now.ToString()); writer.WriteStartElement("UI"); //creating the element writer.WriteElementString("name", sname); writer.WriteElementString("landline",slandline); writer.WriteElementString("question",squestion); writer.WriteElementString("time",stime); writer.WriteElementString("Date",DateTime.Now.ToString()); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close();

    D M 3 Replies Last reply
    0
    • L lehya

      I have a page with data retrieving from user: Name, landline no, Question to ask and Timings..Once i click on button of this page, say callback.aspx, it will generate a xml file automatically with a file name userinfo.xml Now i try to read this file in another .aspx page say read.aspx from userinfo.xml...Everything is working fine..except for.. If another user enters info in callback and submits. And when i check it from read.aspx or userinfo.xml the previous data is lost and the new data is written...Is there any possibility that dynamically data can be appended to userinfo.xml Please help..the following is the code in callback.aspx string xmlFile = Server.MapPath("userInfo.xml"); XmlTextWriter writer = new XmlTextWriter(xmlFile, Encoding.UTF8); //start writing writer.WriteStartDocument(); writer.WriteComment("Posted on @" + DateTime.Now.ToString()); writer.WriteStartElement("UI"); //creating the element writer.WriteElementString("name", sname); writer.WriteElementString("landline",slandline); writer.WriteElementString("question",squestion); writer.WriteElementString("time",stime); writer.WriteElementString("Date",DateTime.Now.ToString()); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close();

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      Well, you've got a few problems to deal with here. First of all is this line:

      lehya wrote:

      XmlTextWriter writer = new XmlTextWriter(xmlFile, Encoding.UTF8);

      This is a shortcut to remove some of the tedium of opening a file. But, its assumption is that you want to erase the old file if one is there, not append to it. The second problem is that you're dealing with an XML file. You can't just append to the end of the file. You have to add your new elements as a part of the existing document. Appending to the end of an XML document is not easy. You have some options though. Open the XML in an XmlDocument object and just append your child nodes in there where appropriate, then save the file. If the XML file is big, this will take up lots of memory and be slow. Another option is to open up the existing file with an XmlReader and open up a temporary file with an XmlWriter. Read the existing XML until you get to the end of the document and while you're doing it, write those elements out to the writer. Before writing the EndDocument, write in your new elements. Then delete the old file and rename the temp file to the correct filename.


      Logifusion[^] If not entertaining, write your Congressman.

      L 1 Reply Last reply
      0
      • D Dustin Metzgar

        Well, you've got a few problems to deal with here. First of all is this line:

        lehya wrote:

        XmlTextWriter writer = new XmlTextWriter(xmlFile, Encoding.UTF8);

        This is a shortcut to remove some of the tedium of opening a file. But, its assumption is that you want to erase the old file if one is there, not append to it. The second problem is that you're dealing with an XML file. You can't just append to the end of the file. You have to add your new elements as a part of the existing document. Appending to the end of an XML document is not easy. You have some options though. Open the XML in an XmlDocument object and just append your child nodes in there where appropriate, then save the file. If the XML file is big, this will take up lots of memory and be slow. Another option is to open up the existing file with an XmlReader and open up a temporary file with an XmlWriter. Read the existing XML until you get to the end of the document and while you're doing it, write those elements out to the writer. Before writing the EndDocument, write in your new elements. Then delete the old file and rename the temp file to the correct filename.


        Logifusion[^] If not entertaining, write your Congressman.

        L Offline
        L Offline
        lehya
        wrote on last edited by
        #3

        Thanks buddy...Sounds like it works..But reading everything and writing into a temporary file...saving it as a new file..oh this sounds too hard..but i will try

        D 1 Reply Last reply
        0
        • L lehya

          Thanks buddy...Sounds like it works..But reading everything and writing into a temporary file...saving it as a new file..oh this sounds too hard..but i will try

          D Offline
          D Offline
          Dustin Metzgar
          wrote on last edited by
          #4

          You could also write to a MemoryStream so long as the XML is not too big. The MemoryStream will have a smaller memory footprint than the XmlDocument object.


          Logifusion[^] If not entertaining, write your Congressman.

          1 Reply Last reply
          0
          • L lehya

            I have a page with data retrieving from user: Name, landline no, Question to ask and Timings..Once i click on button of this page, say callback.aspx, it will generate a xml file automatically with a file name userinfo.xml Now i try to read this file in another .aspx page say read.aspx from userinfo.xml...Everything is working fine..except for.. If another user enters info in callback and submits. And when i check it from read.aspx or userinfo.xml the previous data is lost and the new data is written...Is there any possibility that dynamically data can be appended to userinfo.xml Please help..the following is the code in callback.aspx string xmlFile = Server.MapPath("userInfo.xml"); XmlTextWriter writer = new XmlTextWriter(xmlFile, Encoding.UTF8); //start writing writer.WriteStartDocument(); writer.WriteComment("Posted on @" + DateTime.Now.ToString()); writer.WriteStartElement("UI"); //creating the element writer.WriteElementString("name", sname); writer.WriteElementString("landline",slandline); writer.WriteElementString("question",squestion); writer.WriteElementString("time",stime); writer.WriteElementString("Date",DateTime.Now.ToString()); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close();

            M Offline
            M Offline
            manowj
            wrote on last edited by
            #5

            Hi lehya, First check whether the xml file exists, If not create xml [ u have done this], if exists load xml file, get the root node element and append every node you add as as child node [plz don`t use processing instructions while appending to xml] and save the xml. => Root Element test 35 M test 35 M -- modified at 4:38 Saturday 19th August, 2006 Regards, Manowj

            1 Reply Last reply
            0
            • L lehya

              I have a page with data retrieving from user: Name, landline no, Question to ask and Timings..Once i click on button of this page, say callback.aspx, it will generate a xml file automatically with a file name userinfo.xml Now i try to read this file in another .aspx page say read.aspx from userinfo.xml...Everything is working fine..except for.. If another user enters info in callback and submits. And when i check it from read.aspx or userinfo.xml the previous data is lost and the new data is written...Is there any possibility that dynamically data can be appended to userinfo.xml Please help..the following is the code in callback.aspx string xmlFile = Server.MapPath("userInfo.xml"); XmlTextWriter writer = new XmlTextWriter(xmlFile, Encoding.UTF8); //start writing writer.WriteStartDocument(); writer.WriteComment("Posted on @" + DateTime.Now.ToString()); writer.WriteStartElement("UI"); //creating the element writer.WriteElementString("name", sname); writer.WriteElementString("landline",slandline); writer.WriteElementString("question",squestion); writer.WriteElementString("time",stime); writer.WriteElementString("Date",DateTime.Now.ToString()); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close();

              D Offline
              D Offline
              Dustin Metzgar
              wrote on last edited by
              #6

              You're probably already past this, but I've seen this question enough times that I thought I might write an article on it. Link[^]


              Logifusion[^] If not entertaining, write your Congressman.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups