【发布时间】:2019-06-25 10:56:39
【问题描述】:
我正在尝试将用户输入数据写入 asp.net/C# 中的 XML 文件。 数据将在文本框中输入。
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);
}
没有错误显示 不存储值。
【问题讨论】:
-
请注意,在 ASP.Net 中,您需要对文件进行某种同步来处理并发读/写。数据库或唯一文件可以解决此问题。
-
您正在读写同一个文件。请说明您要达到的目标