[英]how to write data to XML file in asp.net c#?
I am trying to write the user input data to the XML file in asp.net/C#. 我试图将用户输入数据写入asp.net/C#中的XML文件。 the data will type in textbox.
数据将在文本框中键入。
The C# code: C#代码:
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. 没有错误显示没有存储值。
Hope in the below code, runtime error was throwing. 希望在下面的代码中,运行时错误被抛出。
xdoc.Load(Server.MapPath("~/XMLFile1.xml"));
2 reasons for the error. 错误的2个原因。
Basic xml structure: 基本的xml结构:
<?xml version="1.0" encoding="utf-8"?>
<Contacts>
</Contacts>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.