Asp.net 如何将下拉列表中的选定值保存到Xml文件?

Asp.net 如何将下拉列表中的选定值保存到Xml文件?,asp.net,xml,Asp.net,Xml,我的程序中有一个下拉列表。我正在从Xml数据源为dropdownlist绑定数据 <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="C:\Users\rafat\Documents\Visual Studio 2008\frmPdfUpload\Pdf\Test.xml"></asp:XmlDataSource> <asp:DropDownList

我的程序中有一个下拉列表。我正在从Xml数据源为dropdownlist绑定数据

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="C:\Users\rafat\Documents\Visual Studio 2008\frmPdfUpload\Pdf\Test.xml"></asp:XmlDataSource>



     <asp:DropDownList  
             ID="DropDownList2"  
             runat="server"  
             DataSourceID="XmlDataSource1"  
             DataTextField="Name"  
             OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"  
             AutoPostBack="true"  
             BackColor="Bisque"  
             >  
        </asp:DropDownList>  

现在我想将所选值从dropdownlist保存到一个新的xml文件中。我是一个初学者。我不知道有没有可能。提前感谢您的帮助

我已经自己解决了这个问题。代码如下所示:

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string file = Server.MapPath("Customers.xml");

        XmlDocument doc = new XmlDocument();
        XmlNode decNode = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        doc.AppendChild(decNode);

        XmlNode parentNode = doc.CreateElement("Customers");
        doc.AppendChild(parentNode);

        XmlNode childNode1 = doc.CreateElement("Customer");
        XmlAttribute name = doc.CreateAttribute("name");
        name.Value = DropDownList1.SelectedItem.ToString();
        childNode1.Attributes.Append(name);
        parentNode.AppendChild(childNode1);

        doc.Save(file);
        Label1.Text = "XML File Created and data inserted successfully";
    }

您只需在项目文件夹中创建一个Xml文件名Customers.Xml

我认为将值保存到单个Xml文件不是一个好的选择,保存到app.config更好,因为它既简单又干净

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
configuration.AppSettings.Settings["Name"].Value = DropDownList1.SelectedItem.ToString();  
configuration.Save(ConfigurationSaveMode.Modified);   
参考-