如何将数据插入asp.net中的现有xml文件?

我正在使用Visual Web Developer 2008 Express Edition,因为我是新手,所以我需要你的帮助。 我正在尝试将数据插入或写入我的xml文件,以便我可以将其显示到我的xml控件中。 现在,我想在这里做的是每当用户在文本框中输入一条消息时,他都可以选择保存它,这样如果他点击命令按钮,我想将文本消息从文本框保存到我的xml的任何元素中文件。 比方说,我想将它插入我的xml文件的元素中。 如何使用C#或VB.Net代码执行此操作? 我有我的xml文件和我的C#代码,但c#代码对我不起作用。 我需要c#或vb.net中的代码,无论哪种方式都适合我。 非常感谢,非常感谢任何可以分享的帮助。

myxmlfile.xml

   Your Comments Here: Please post your comments now. Thank you.   Note: Please do not post any profane languages.   I don't like their service. It's too lousy.   Always be alert on your duty. Don't be tardy enough to waste your time.   

 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和@AVD – 非常感谢你的回复。 该代码将在myxmlfile中添加一个新的根元素,因此问题是当重新加载页面时新的根元素没有显示在我的xml控件上,因为我的xslt文件中没有包含新的根元素以在xml上显示myxmlfile控制。 我不想在myxmlfile上添加新的根元素。 我只想将从文本框输入的消息插入到myxmlfile的现有元素中,这是元素,以便它可以在xml控件上显示,我打算显示myxmlfile。 我希望你能再次为我修改代码。 非常感谢你的帮助。 我非常感谢。

您必须使用MapPath()指定绝对文件路径以保存XML文档,并且不要增加标记名称,如comment1,comment2 ..等。

看看代码片段:

 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); } 

编辑:如果要将标记插入到现有的xml文档中,则无需创建XDocument。 只需加载现有文档并在根目录下添加新元素即可。

 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); } 

要在添加另一个标记:

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

要替换标记的文本值:

 doc.Root.Element("comment").Value = TextBox1.Text; //doc.Root.Element("comment").Value += TextBox1.Text; //append text doc.Save(file); 

XML文档:

    First Child   Nested   

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

在此处输入图像描述

像这样设计页面。在按钮单击事件中写下面的代码

 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(); } 

并将事件标记放在xml文件中。 现在运行应用程序并检查输出