c# – 如何将数据插入asp.net中的现有xml文件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何将数据插入asp.net中的现有xml文件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2984字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何将数据插入asp.net中的现有xml文件?](/upload/InfoBanner/zyjiaocheng/808/911dacc925d349499ccf6980dca62673.jpg)
我正在使用Visual Web Developer 2008 Express Edition,因为我是新手,所以我需要你的帮助.我正在尝试将数据插入或写入我的xml文件,以便我可以将其显示到我的xml控件中.现在,我在这里尝试做的是每当用户在文本框中输入一条消息时,他都可以保存它,所以如果他点击命令按钮我想将文本消息从文本框保存到我的xml的任何元素中文件.比方说,我想将它插入我的xml文件的元素中.如何使用C#或VB.Net代码执行此操作?我有下面的xml文件和我的C#代码,但c#代码对我不起作用.我需要c#或vb.net中的代码,无论哪种方式都适合我.
非常感谢,非常感谢任何可以分享的帮助.
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>
码
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);
}
编辑:如果你想插入< comment>标记到现有的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);
}
要添加其他< comment>标签内< comment>:
XElement ele = new XElement("comment",TextBox1.Text);
doc.Root.Element("comment").Add(ele);
doc.Save(file);
要替换< comment>的文本值标签:
doc.Root.Element("comment").Value = TextBox1.Text;
//doc.Root.Element("comment").Value += TextBox1.Text; //append text
doc.Save(file);
XML文档:
<?xml version="1.0" encoding="utf-8" ?>
<comments> <!-- Root Node -->
<comment>First Child</comment>
<comment> <!-- Second Child -->
<comment>Nested</comment>
</comment>
</comments>
内容总结
以上是互联网集市为您收集整理的c# – 如何将数据插入asp.net中的现有xml文件?全部内容,希望文章能够帮你解决c# – 如何将数据插入asp.net中的现有xml文件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。