How to insert data into an existing xml file in asp.net?

I am using Visual Web Developer 2008 Express Edition and I need your help as I am new to this. I am trying to insert or write data to my XML file so that I can display it in my xml control. Now, what I'm trying to do here, every time the user enters a message in a text field, he has the option to save it, so if he clicks on the command button, I want to save the text message from the text field to any elements of my xml file. Let's say I want to insert it into an element of my xml file. How to do this using C # or VB.Net codes? I have my xml file below and my C # code, but C # code does not work for me. I need code for this either in C # or in vb.net, any way will work for me. Thank you so much, and I greatly appreciate any help that can be provided.

myxmlfile.xml

<?xml version="1.0" encoding="utf-8" ?> <comments> <comment> Your Comments Here: Please post your comments now. Thank you. </comment> <comment2> Note: Please do not post any profane languages. </comment2> <comment3> I don't like their service. It too lousy. </comment3> <comment4> Always be alert on your duty. Don't be tardy enough to waste your time. </comment4> </comments> 

code

 protected void Button1_Click(object sender, EventArgs e) { System.Xml.Linq.XDocument mydoc = new System.Xml.Linq.XDocument( new System.Xml.Linq.XDeclaration("1.0", "UTF-8", "yes"), new System.Xml.Linq.XElement("comment", new System.Xml.Linq.XComment(TextBox1.Text))); mydoc.Save("myxmlfile.xml",System.Xml.Linq.SaveOptions .None); } 

@Joseph LeBrech and @AVD - Thank you very much for your reply. This code will add a new root element to myxmlfile, so the problem is that the new root element does not appear on my xml control when the page reloads because the new root element is not included in my xslt file to show myxml file in xml control . I do not want to add a new root element to myxmlfile. I just want to paste the messages entered from the text box into the existing myxmlfile element, which is the element so that it can be displayed in the xml control where I intend to display the myxml file. Hope you can change the code again for me. Thank you so much for your help. I really appreciated that.

+4
source share
3 answers

You must specify the absolute path to the file using MapPath () in order to save the XML document and not increase the tag name, for example comment1, comment2 .., etc.

Take a look at the code snippet:

 protected void Button1_Click(object sender, EventArgs e) { string file = MapPath("~/comments.xml"); XDocument doc; //Verify whether a file is exists or not if (!System.IO.File.Exists(file)) { doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new System.Xml.Linq.XElement("comments")); } else { doc = XDocument.Load(file); } XElement ele = new XElement("comment",TextBox1.Text); doc.Root.Add(ele); doc.Save(file); } 

EDIT: if you want to insert the <comment> into an existing xml document, then you do not need to create an XDocument. Just upload an existing document and add a new item to the root directory.

 protected void Button1_Click(object sender, EventArgs e) { string file = MapPath("~/myxmlfile.xml"); XDocument doc = XDocument.Load(file); XElement ele = new XElement("comment",TextBox1.Text); doc.Root.Add(ele); doc.Save(file); } 

To add another <comment> inside <comment> :

  XElement ele = new XElement("comment",TextBox1.Text); doc.Root.Element("comment").Add(ele); doc.Save(file); 

To replace the text value of the <comment> :

 doc.Root.Element("comment").Value = TextBox1.Text; //doc.Root.Element("comment").Value += TextBox1.Text; //append text doc.Save(file); 

XML document:

 <?xml version="1.0" encoding="utf-8" ?> <comments> <!-- Root Node --> <comment>First Child</comment> <comment> <!-- Second Child --> <comment>Nested</comment> </comment> </comments> 
+5
source

myDoc.Element("comments").Add(new xElement("comment5") { Value = "put the value in here"});

+2
source

enter image description here

Create a page like this. In the button click event write the following code

 protected void btnInsert_Click(object sender, EventArgs e) { System.Xml.XmlDocument myXml = new System.Xml.XmlDocument(); myXml.Load(Server.MapPath("InsertData.xml")); System.Xml.XmlNode xmlNode = myXml.DocumentElement.FirstChild; System.Xml.XmlElement xmlElement = myXml.CreateElement("entry"); xmlElement.SetAttribute("Name", Server.HtmlEncode(txtname.Text)); xmlElement.SetAttribute("Location", Server.HtmlEncode(txtlocation.Text)); xmlElement.SetAttribute("Email", Server.HtmlEncode(txtemail.Text)); xmlElement.SetAttribute("Gender", Server.HtmlEncode(ddlgender.SelectedItem.Text)); myXml.DocumentElement.InsertBefore(xmlElement,xmlNode); myXml.Save(Server.MapPath("InsertData.xml")); BindData(); lbldisplay.Text = "Record inserted into XML file successfully"; txtname.Text = ""; txtlocation.Text = ""; txtemail.Text = ""; } private void BindData() { XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("InsertData.xml")); xmlReader.Close(); } 

as well as place event tags in an xml file. Now run the application and check the output

+1
source

Source: https://habr.com/ru/post/1384873/


All Articles