.net读写xml文档详解(3)


//设置配置文件物理路径
    public string xmlPath = "/manage/spider/config.xml";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置程序物理路径+文件物理路径
            string path = Request.PhysicalApplicationPath + xmlPath;
            //获取XML元素对象
            XElement config = XElement.Load(path);
            if (config != null)
            {
                //获得节点子元素
                XElement eleAmazonDetailUrl = config.Element("AmazonDetailUrl");
                XElement eleAmazonListUrl = config.Element("AmazonListUrl");
                XElement eleHz = config.Element("Hz");
                XElement eleCount = config.Element("Count");
                //在页面上呈现取到的数据
                if (eleAmazonDetailUrl != null)
                    TextBox_AmazonDetailUrl.Text = eleAmazonDetailUrl.Value;
                if (eleAmazonListUrl != null)
                    TextBox_AmazonListUrl.Text = eleAmazonListUrl.Value;
                if (eleHz != null)
                    TextBox_Hz.Text = eleHz.Value;
                if (eleCount != null)
                    TextBox_Count.Text = eleCount.Value;
            }
            else
                Response.Write("");

}
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //设置XML文件路径
        string path = Request.PhysicalApplicationPath + xmlPath;
        //设置节点的名称和内容
        XElement root = new XElement("Settings",
             new XElement("AmazonDetailUrl", TextBox_AmazonDetailUrl.Text.Trim()),
             new XElement("AmazonListUrl", TextBox_AmazonListUrl.Text.Trim()),
             new XElement("Hz", TextBox_Hz.Text.Trim()),
             new XElement("Count", TextBox_Count.Text.Trim())
                 );
        //将元素序列化到指定路径的XML文件当中
        root.Save(path);
     }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzfjf.html