0

I have an existing xml file that I want to use to store the contents of a submission form.

/**** the XML file StoreUserInfo.xml:

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

</recordstore>

/****** the form

<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>

/********** the source code

//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";
    }

... I'm not getting any errors, but the form fails to write the contents of the form to the xml file when the button is clicked. Could someone provides some assistance as to what I'm missing?

4

1 回答 1

0

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

于 2013-07-25T17:19:06.193 回答