C#里Winform读取XML配置文件

dy360mpt7eucig 2009-12-30 08:28:50
我做了一个小软件,但是用户设置每次都要选择,我希望点击保存设置按钮时,将用户设置以键值对的方式保存到用户电脑的XML文件里,但是我不会呀,要怎样向里写,又怎样读出来呢,还有,怎样先判断配置文件是否存在与用户的电脑中,希望得到大侠的帮助~~~~(>_<)~~~~
...全文
497 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
daichenghua 2009-12-30
  • 打赏
  • 举报
回复
var xmlDoc;
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false"
xmlDoc.load("xml\\"+temp123);
var nodes=xmlDoc.documentElement.selectNodes("basicinfo");

for(i=0;i<nodes.length;i++)
{
var name = nodes[i].childNodes[0].text;

var lat = nodes[i].childNodes[2].text; // alert(lat);
var lng = nodes[i].childNodes[3].text;
var info =nodes[i].childNodes[0].text+ "<br/>";
}
----------读取
周公 2009-12-30
  • 打赏
  • 举报
回复
读可以按照节点名利用ConfigurationManager类来读,如ConfigurationManager.AppSettings["Name1"]
写的示例代码如下:
#     protected void btnModify_Click(object sender, EventArgs e)
# {
# string appSetting = txtAppSetting.Text;//appSetting子节点值
# string connectionString = txtConnectionString.Text;//连接字符串
# string key = txtKey.Text;//appSetting子节点Key
# string connectionName = txtConnectionName.Text;//连接Name
# ConfigurationOperator op = new ConfigurationOperator();
# op.SetAppSetting(key, appSetting);
# op.SetConnectionString(connectionName, connectionString);
# op.Save();
# }
daichenghua 2009-12-30
  • 打赏
  • 举报
回复
string xmlUrl = Server.MapPath("") + @"\xml\Info.xml";
XmlDocument doc = new XmlDocument(); // 创建dom对象
XmlElement root = doc.CreateElement("root"); // 创建根节点data
doc.AppendChild(root); // 加入到xml document
doc.Save(xmlUrl); // 保存文件


XmlTextWriter xtw = new XmlTextWriter(xmlUrl, null);
xtw.Formatting = Formatting.Indented; //缩进格式
xtw.Indentation = 4;

xtw.WriteStartDocument();
xtw.WriteStartElement("root");


string sql = "";

sql = "";

DataSet ds = new DataSet(); ;
DataSet ds2 = new DataSet(); ;
ds = MySqlDBUtil.ExecuteQuery(sql);
DataView dvTree = new DataView();
dvTree = ds.Tables[0].DefaultView;
dvTree.Sort = "tra_id";

foreach (DataRowView Row in dvTree)
{




xtw.WriteStartElement("node");
xtw.WriteAttributeString("id", Row["tra_id"].ToString());
xtw.WriteStartElement("code");
xtw.WriteString(Row["tra_id"].ToString());
xtw.WriteEndElement();
xtw.WriteStartElement("name");
xtw.WriteString(Row["tra_name"].ToString());
xtw.WriteEndElement();
xtw.WriteStartElement("icon");
xtw.WriteString("images/d.gif");
xtw.WriteEndElement();
xtw.WriteEndElement();
sql = "";
ds2 = new DataSet(); ;
ds = MySqlDBUtil.ExecuteQuery(sql);

DataView dvTree2 = new DataView();
dvTree2 = ds.Tables[0].DefaultView;
dvTree2.Sort = "tra_id";
foreach (DataRowView Row2 in dvTree2)
{
xtw.WriteStartElement("node");
xtw.WriteAttributeString("id", Row2["tra_id"].ToString());
xtw.WriteAttributeString("parentid", Row["tra_id"].ToString());
xtw.WriteStartElement("code");
xtw.WriteString(Row2["tra_id"].ToString());
xtw.WriteEndElement();
xtw.WriteStartElement("name");
xtw.WriteString(Row2["tra_name"].ToString());
xtw.WriteEndElement();
xtw.WriteStartElement("icon");
xtw.WriteString("images/d.gif");
xtw.WriteEndElement();
xtw.WriteEndElement();
}




}
xtw.Flush();
xtw.Close();

--创建
周公 2009-12-30
  • 打赏
  • 举报
回复
在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代码再重新编译,这种部署方式非常麻烦。
在.net 中提供了一种便捷的保存项目配置信息的办法,那就是利用配置文件,配置文件的文件后缀一般是.config,在asp.net中配置文件名一般默认是 web.config。每个web.config文件都是基于XML的文本文件,并且可以保存到Web应用程序中的任何目录中。在发布Web应用程序时 web.config文件并不编译进dll文件中。如果将来客户端发生了变化,仅仅需要用记事本打开web.config文件编辑相关设置就可以重新正常使用,非常方便。

配置文件的读写操作
虽然web.config文件是一个XML文件,但是由于权限的原因它在部署中不能像操作普通XML文件那样进行修改,在.net中提供了一个类用于对web.config进行修改。
下面是针对web.config修改通用类的代码:




1. using System;
2. using System.Configuration;
3. using System.Web;
4. using System.Web.Configuration;
5. /// <summary>
6. /// ConfigurationOperator 的摘要说明
7. /// </summary>
8. public class ConfigurationOperator:IDisposable
9. {
10. private Configuration config;
11. public ConfigurationOperator():this(HttpContext.Current.Request.ApplicationPath)
12. {
13.
14. }
15. public ConfigurationOperator(string path)
16. {
17. config = WebConfigurationManager.OpenWebConfiguration(path);
18. }
19. /// <summary>
20. /// 设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点
21. /// </summary>
22. /// <param name="key">节点名称</param>
23. /// <param name="value">节点值</param>
24. public void SetAppSetting(string key, string value)
25. {
26. AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
27. if (appSetting.Settings[key] == null)//如果不存在此节点,则添加
28. {
29. appSetting.Settings.Add(key, value);
30. }
31. else//如果存在此节点,则修改
32. {
33. appSetting.Settings[key].Value = value;
34. }
35. }
36. /// <summary>
37. /// 设置数据库连接字符串节点,如果不存在此节点,则会添加此节点及对应的值,存在则修改
38. /// </summary>
39. /// <param name="key">节点名称</param>
40. /// <param name="value">节点值</param>
41. public void SetConnectionString(string key, string connectionString)
42. {
43. ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings");
44. if (connectionSetting.ConnectionStrings[key] == null)//如果不存在此节点,则添加
45. {
46. ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(key, connectionString);
47. connectionSetting.ConnectionStrings.Add(connectionStringSettings);
48. }
49. else//如果存在此节点,则修改
50. {
51. connectionSetting.ConnectionStrings[key].ConnectionString = connectionString;
52. }
53. }
54. /// <summary>
55. /// 保存所作的修改
56. /// </summary>
57. public void Save()
58. {
59. config.Save();
60. config = null;
61. }
62. public void Dispose()
63. {
64. if (config != null)
65. {
66. config.Save();
67. }
68. }
69. }
周公 2009-12-30
  • 打赏
  • 举报
回复
用config文件好了,在网站中是web.config,在应用程序中是app.config,二者的本质都是XML文件,只不过有些特殊格式要求,利用现成的类就可以读取。
《asp.net夜话之十一:web.config详解》
柳晛 2009-12-30
  • 打赏
  • 举报
回复
dataset.WriteXml("D:\\1.xml");

dataset.ReadXml("D:\\1.xml");
柳晛 2009-12-30
  • 打赏
  • 举报
回复
你把数据存放在DataSet里面,这个类的的实例有读取、保存 XML的方法,非常方便!
zhucemajiaburongyi 2009-12-30
  • 打赏
  • 举报
回复

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("EmailList.xml");

XmlNode xmldocSelect = xmlDoc.SelectSingleNode("user");
XmlElement el = xmlDoc.CreateElement("person");
el.SetAttribute("sCode", "这里是前台传过来的");
el.SetAttribute("sPwd","这里是前台传过来的");
xmldocSelect.AppendChild(el);
xmlDoc.Save("EmailList.xml");
}

}


<?xml version="1.0" encoding="utf-8"?>
<user>
<person sCode="账号" sPwd="密码" />
</user>



代码太多了

懒的贴了

只给写XML的 吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧