New to XML
-
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();
-
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();
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.
-
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.
-
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
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.
-
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();
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
-
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();
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.