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

tim*_*ack 5 c# xml asp.net

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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

ada*_*ost 5

您必须使用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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果要将<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);
}
Run Code Online (Sandbox Code Playgroud)

要在<comment>里面添加另一个标签<comment>:

 XElement ele = new XElement("comment",TextBox1.Text);
 doc.Root.Element("comment").Add(ele);
 doc.Save(file);
Run Code Online (Sandbox Code Playgroud)

要替换<comment>标记的文本值:

doc.Root.Element("comment").Value = TextBox1.Text;
//doc.Root.Element("comment").Value += TextBox1.Text; //append text
doc.Save(file);
Run Code Online (Sandbox Code Playgroud)

XML文档:

<?xml version="1.0" encoding="utf-8" ?>
<comments> <!-- Root Node -->
  <comment>First Child</comment>
  <comment> <!-- Second Child -->
    <comment>Nested</comment>
  </comment>
</comments>
Run Code Online (Sandbox Code Playgroud)