将提交表单内容存储到挂起的xml文件中

时间:2013-07-19 16:06:31

标签: asp.net

我有一个现有的xml文件,我想用它来存储提交表单的内容。

/ * ** * XML文件StoreUserInfo.xml:

<?xml version="1.0" encoding="utf-8" ?>
<recordstore>

</recordstore>

/ * ** * ** 表格

<div>Genre: <asp:TextBox ID="txtGenre" runat="server" /></div><br />
        <div>Title: <asp:TextBox ID="txtTitle" runat="server" /></div><br />
        <div>Name: <asp:TextBox ID="txtName" runat="server" /></div><br />
        <div>Email: <asp:TextBox ID="txtEmail" runat="server" /></div><br />
        <div>Comments: <br /><asp:TextBox ID="txtComment" runat="server" Width="200px" Height="100px" /></div><br />
        <div><asp:Button ID="btnSubmit" runat="server" Text="submit" 
                onclick="btnSubmit_Click" /></div><br />
        <div>
            <asp:Label ID="lblResult" runat="server" />
        </div>

/ * ** * ** * ** * 源代码

//function to create the nodes
    XmlNode CreateBookNode(XmlDocument doc)
    {
        //Create the author node and its children
        XmlNode recordNode = doc.CreateElement("record");  
        XmlAttribute genreAttribute = doc.CreateAttribute("genre");
        genreAttribute.Value = txtGenre.Text;
        recordNode.Attributes.Append(genreAttribute);

        //Create title attribute and add all the children of the record node
        XmlNode titleNode = doc.CreateElement("title");
        titleNode.InnerText = txtTitle.Text;
        recordNode.AppendChild(titleNode);

        //Create the Author Node and it's children
        XmlNode authorNode = doc.CreateElement("author");
        XmlNode FullNameNode = doc.CreateElement("FullName");
        FullNameNode.InnerText = txtName.Text;
        authorNode.AppendChild(FullNameNode); 

        //now add email 
        XmlNode emailNode = doc.CreateElement("Email");
        emailNode.InnerText = txtEmail.Text;
        authorNode.AppendChild(emailNode);  

        recordNode.AppendChild(authorNode); 

        //now add comments
        XmlNode commentNode = doc.CreateElement("Comments");
        commentNode.InnerText = txtComment.Text;
        recordNode.AppendChild(commentNode); 

       return recordNode;
    }

    //button click
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //path to the xml file
        string xmlPath = MapPath("StoreUserInfo.xml");
        XmlDocument doc = new XmlDocument();

        doc.Load(xmlPath);
        XmlNode recordNode = CreateBookNode(doc); 
        //Get reference to the book node and append the book node to it
        XmlNode recordStoreNode = doc.SelectSingleNode("recordstore"); 
        recordStoreNode.AppendChild(recordNode);             
        lblResult.Text = "XML Document has been successfully updated";
    }

...我没有收到任何错误,但单击按钮时表单无法将表单内容写入xml文件。有人能为我所缺少的东西提供一些帮助吗?

1 个答案:

答案 0 :(得分:0)

我得到了它的工作;我的btnsubmit_Click函数缺少doc.Save(xmlPath);