1 //設置配置文件物理路徑 2 public string xmlPath = "/manage/spider/config.xml"; 3 PRotected void Page_Load(object sender, EventArgs e) 4 { 5 if (!IsPostBack) 6 { 7 //設置程序物理路徑+文件物理路徑 8 string path = Request.PhysicalapplicationPath + xmlPath; 9 //獲取XML元素對象10 XElement config = XElement.Load(path);11 if (config != null)12 {13 //獲得節點子元素14 XElement eleAmazonDetailUrl = config.Element("AmazonDetailUrl");15 XElement eleAmazonListUrl = config.Element("AmazonListUrl");16 XElement eleHz = config.Element("Hz");17 XElement eleCount = config.Element("Count");18 //在頁面上呈現取到的數據19 if (eleAmazonDetailUrl != null)20 TextBox_AmazonDetailUrl.Text = eleAmazonDetailUrl.Value;21 if (eleAmazonListUrl != null)22 TextBox_AmazonListUrl.Text = eleAmazonListUrl.Value;23 if (eleHz != null)24 TextBox_Hz.Text = eleHz.Value;25 if (eleCount != null)26 TextBox_Count.Text = eleCount.Value;27 }28 else29 Response.Write("");30 31 }32 }33 protected void btn_Save_Click(object sender, EventArgs e)34 {35 //設置XML文件路徑36 string path = Request.PhysicalApplicationPath + xmlPath;37 //設置節點的名稱和內容38 XElement root = new XElement("Settings",39 new XElement("AmazonDetailUrl", TextBox_AmazonDetailUrl.Text.Trim()),40 new XElement("AmazonListUrl", TextBox_AmazonListUrl.Text.Trim()),41 new XElement("Hz", TextBox_Hz.Text.Trim()),42 new XElement("Count", TextBox_Count.Text.Trim())43 );44 //將元素序列化到指定路徑的XML文件當中45 root.Save(path);46 }
新聞熱點
疑難解答