0

I am trying to write the user input data to the XML file in asp.net/C#. the data will type in textbox.

The C# code:

int i = 0;
protected void submit(object sender,EventArgs e)
{
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(Server.MapPath("~/XMLFile1.xml"));
    Console.WriteLine(xdoc);
    XmlElement contact = xdoc.CreateElement("Contact");

    XmlAttribute Count = xdoc.CreateAttribute("Count");
    i = i + 1;
    Count.Value = i.ToString();

    XmlElement Name = xdoc.CreateElement("Name");
    XmlText xmlName = xdoc.CreateTextNode(name.Text);

    XmlElement EmailId = xdoc.CreateElement("EmailId");
    XmlText xmlemail = xdoc.CreateTextNode(emailId.Text);

    XmlElement Comments = xdoc.CreateElement("Comments");
    XmlText xmlcomment = xdoc.CreateTextNode(comments.Text);

    Name.AppendChild(xmlName);
    EmailId.AppendChild(xmlemail);
    Comments.AppendChild(xmlcomment);

    contact.Attributes.Append(Count);
    contact.AppendChild(Name);
    contact.AppendChild(EmailId);
    contact.AppendChild(Comments);

    xdoc.DocumentElement.AppendChild(contact);

    xdoc.Save(Server.MapPath("~/XMLFile1.xml"));

    Response.Redirect(Request.Url.AbsoluteUri);
}

No errors are displaying not storing the values.

3

1 Answer 1

0

Hope in the below code, runtime error was throwing.

xdoc.Load(Server.MapPath("~/XMLFile1.xml"));

2 reasons for the error.

  1. XMLFile1.xml not available in the root directory.
  2. Basic xml structure not available in the xml file.

Basic xml structure:

<?xml version="1.0" encoding="utf-8"?>
<Contacts>

</Contacts>
Sign up to request clarification or add additional context in comments.

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.