5

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.

3
  • 3
    you shouldn't be incrementing your comment tags like this. you could put a name or id attribute or even a date for sorting purposes but incrementing your tag is asking for trouble. Commented Dec 6, 2011 at 10:10
  • One reason not to do comment tags like this is that there's no sane way to describe it in a DTD. Comment order is already implied by the order of the comments in the file. Commented Dec 6, 2011 at 13:44
  • Don't reply by editing an answer. Either use "add comment" under the answer or edit your question to provide additional information. Commented Dec 6, 2011 at 13:48

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

6 Comments

@AVD-----------okay, the code above you gave still add a new root element on the myxmlfile. I want to add or insert the data into the existing <comment> element of the myxmlfile. Please look at the original myxml file above. <comment> Your Comments Here: Please post your comments now. Thank you. </comment>
@AVD-----------okay, the code above you gave still add a new root element on the myxmlfile. I want to add or insert the data into the existing <comment> element of the myxmlfile. Please look at the original myxmlfile above.<comment>Your Comments Here: Please post your comments now. Thank you.</comment> inside on that existing element that's where I want to insert the data so that it can be shown in the xml control to display the data. Because if you add a new root element it will not be shown in the xml control because its not included in my xslt file. Is that possible? Thank you.
@timmack - Sorry! Do you want to insert/append text into the <comment> tags. Isn't that? See my edited post.
@AVD-----------Hey! That's it, you got what I'm looking for. You did pretty well on your last post, you differentiate it. Thank you very much for your help AVD. You did an excellent job. I hope I can always talk a guy like u.
Again --- Hey AVD, I have one last simple concern. I noticed that using the command button's postbackurl properties to navigate to another webpage will do it in just one click but if you clear the properties and put a code inside the click eventhandler which is like this button1.postbackurl = "~/Mythirdwebpage.aspx"; it needs two clicks before it navigate on that page. Why is that? How do I make it in just one click using the code to navigate into another webpage without setting the postbackurl property of the command button?
|
2

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

Comments

1

enter image description here

Design the page like this.In 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();
    }

and also put events tag in xml file. Now run the application and check the output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.