Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

xml

asp.net

I'm using Visual Web Developer 2008 Express Edition and I need your assistance since I'm new to it. I'm trying to insert or write a data to my xml file so that I can display it into my xml control. Now, what I'm trying to do here is everytime the user enter a message into the textbox he has an option to save it so if he clicks the command button I want to save the text message from the textbox into any elements of my xml file. Let say, I want to insert it in the element of my xml file. How do I do it using C# or VB.Net codes? I have my xml file below and my C# code but the c# code doesn't work for me. I need the code for that either in c# or vb.net, either way will work for me. Thank you very much and I greatly appreciate any help that could be shared.

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's 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. That code would add a new root element in myxmlfile so the problem is that the new root element is not showing on my xml control when the page is reloaded because the new root element is not included on my xslt file to show the myxmlfile on the xml control. I don't want to add a new root element on myxmlfile. I just want to insert the messages entered from the textbox into the existing element of myxmlfile which is the element so that it can be shown on the xml control where i intend to display the myxmlfile. I hope you can modify the code for me again. Thank you very for your help. I greatly appreciated it.

like image 253
timmack Avatar asked Dec 06 '11 09:12

timmack


People also ask

How do I add data to an XML file?

To insert data into an XML column, use the SQL INSERT statement. The input to the XML column must be a well-formed XML document, as defined in the XML 1.0 specification. The application data type can be an XML, character, or binary type.


2 Answers

You have to specify the absolute file path using MapPath() to save the XML document and don't increment tag name like comment1,comment2.. etc.

Take a look at 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 <comment> tag into existing xml document then no need to create XDocument. Just load the existing document and add a new element at the root.

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> tag inside <comment>:

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

To replace the text value of <comment> tag:

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>
like image 171
KV Prajapati Avatar answered Sep 20 '22 02:09

KV Prajapati


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

like image 30
Joseph Le Brech Avatar answered Sep 19 '22 02:09

Joseph Le Brech