• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
悲伤的穿山甲
跟我做朋友说明你很有眼光.
博客园    首页    新随笔    联系   管理    订阅  订阅
.Net操作XML文件

 

 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             else
29                 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      }

 

签名。。
posted on 2014-03-25 10:52  悲伤的穿山甲  阅读(623)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3