如何将数据插入到asp.net现有的XML文件? [英] How to insert data into an existing xml file in asp.net?

查看:119
本文介绍了如何将数据插入到asp.net现有的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Visual Web Developer 2008 Express版本,我需要你的帮助,因为我是新来的吧。我试图插入或写入数据到我的XML文件,这样我可以显示它变成我的XML的控制。现在,我想在这里做的是每次用户输入信息到他有一个选项,所以,如果他点击命令按钮我想从文本保存文本信息到我的任何XML元素保存文本框文件。让说,我想将它插入我的XML文件中的元素。我该怎么做使用C#或VB.Net代码呢?下面我有我的XML文件,我的C#代码,但C#代码不为我工作。我需要的代码无论是在C#或vb.net,无论哪种方式会为我工作。
非常感谢你和我非常感谢可以分享任何帮助。

I'm using Visual Web Developer 2008 Express Edition and I need your assistance since I'm new to it. I'm trying to insert or write a data to my xml file so that I can display it into my xml control. Now, what I'm trying to do here is everytime the user enter a message into the textbox he has an option to save it so if he clicks the command button I want to save the text message from the textbox into any elements of my xml file. Let say, I want to insert it in the element of my xml file. How do I do it using C# or VB.Net codes? I have my xml file below and my C# code but the c# code doesn't work for me. I need the code for that either in c# or vb.net, either way will work for me. Thank you very much and I greatly appreciate any help that could be shared.

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 --Thank你非常的回复。所以问题是,由于不包括在我的XSLT文件,新的根元素,以显示对XML的myxmlfile新的根元素没有显示在我的XML控制当页面重新加载该代码将在myxmlfile添加新的根元素控制。我不想上myxmlfile添加新的根元素。我刚要插入从文本输入到这是该元件,以便它可以在XML控制,其中i打算显示myxmlfile显示myxmlfile的现有元素的消息。我希望你可以再次修改代码我。非常感谢您的帮助。我非常感谢它。

@Joseph LeBrech and @AVD --Thank you very much for your reply. That code would add a new root element in myxmlfile so the problem is that the new root element is not showing on my xml control when the page is reloaded because the new root element is not included on my xslt file to show the myxmlfile on the xml control. I don't want to add a new root element on myxmlfile. I just want to insert the messages entered from the textbox into the existing element of myxmlfile which is the element so that it can be shown on the xml control where i intend to display the myxmlfile. I hope you can modify the code for me again. Thank you very for your help. I greatly appreciated it.

推荐答案

您必须指定使用的MapPath()来保存XML文档和Don'文件的绝对路径。T增量标记名称,比如注释1,注释2 ..等等

You have to specify the absolute file path using MapPath() to save the XML document and don't increment tag name like comment1,comment2.. etc.

看看代码片段:

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。只是加载现有文档,并在根部添加新元素。

If you want to insert <comment> tag into existing xml document then no need to create XDocument. Just load the existing document and add a new element at the root.

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

要添加其他<意见>

To add another <comment> tag inside <comment>:

 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文档:

XML document :

<?xml version="1.0" encoding="utf-8" ?>
<comments> <!-- Root Node -->
  <comment>First Child</comment>
  <comment> <!-- Second Child -->
    <comment>Nested</comment>
  </comment>
</comments>

这篇关于如何将数据插入到asp.net现有的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆