这样的XML文件该怎么读?

南风1 2008-03-18 04:31:49
如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="NetSelect" value="1"/>
<add key="NetDialPortSel" value="COM1"/>
<add key="ElecScalePortSel" value="COM2"/>
<add key="CallbackPortSel" value="COM3"/>
<add key="ReadCardPortSel" value="USB"/>
</appSettings>
</configuration>

我的方法:
string sNetSel = System.Configuration.ConfigurationSettings.AppSettings["NetSelect"];
string sDialSel = System.Configuration.ConfigurationSettings.AppSettings["NetSelect"];
string sElecSel = System.Configuration.ConfigurationSettings.AppSettings["NetSelect"];
string sCallSel = System.Configuration.ConfigurationSettings.AppSettings["NetSelect"];
string sReadSel = System.Configuration.ConfigurationSettings.AppSettings["NetSelect"];
读取出来的是正确的值,当改变value的值后读取的值还是原来的值,是不是这种读的方式不可行?
...全文
95 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
南风1 2008-03-18
  • 打赏
  • 举报
回复
谢谢alanfoxye和楼上的几位朋友 这个方法很不错哦,感谢
alanfoxye 2008-03-18
  • 打赏
  • 举报
回复
读xml文件


XmlDocument document= new XmlDocument();
document.Load("App.config文件路径和文件名");

string NetSelect_Value = document.SelectSingleNode(@"//appSettings/add[@key=""NetSelect""]").Attributes["value"].Value;

南风1 2008-03-18
  • 打赏
  • 举报
回复
to alanfoxye:
请问有什么简单方法可以直接从文件中读取?
沃尔特容易 2008-03-18
  • 打赏
  • 举报
回复
可能后来的时候System.Configuration.ConfigurationSettings.AppSettings是从内存中读的。
你如果自己写一个读取的方法调用就好了,下面是我的一个方法,你参考一下吧。你的config文件没有名称空间,你可以把下下面的代码删除几行,就可以了。
ns_xpath的格式是//ns:add[@key='CallbackPortSel']
string sel = GetWebConfigParam("//ns:add[@key='CallbackPortSel']","value");

public static string GetWebConfigParam(string ns_xpath, string name)
{
string basedir = System.AppDomain.CurrentDomain.BaseDirectory;
if (!basedir.EndsWith("\\"))
basedir += "\\";
string basefile += "Web.config";

if(!System.IO.File.Exists(basefile)) basefile = "";
if (basefile != "")
{
try
{
string webconfigfile = basefile;
if (System.IO.File.Exists(webconfigfile))
{
System.Xml.XmlDocument webconfigdoc = new System.Xml.XmlDocument();
webconfigdoc.Load(webconfigfile);
System.Xml.XmlNamespaceManager m = new System.Xml.XmlNamespaceManager(webconfigdoc.NameTable);
m.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
System.Xml.XmlElement el = (System.Xml.XmlElement)webconfigdoc.SelectSingleNode(ns_xpath, m);
if (name == null || name.Trim() == "")
return el.InnerText;
else
{
name = name.Trim();
if (el.HasAttribute(name))
return el.GetAttribute(name);
}
}
}
catch
{
}

}
return "";
}

wdz_77 2008-03-18
  • 打赏
  • 举报
回复
XmlDocument doc = new XmlDocument();
string PathXML1 = Server.MapPath(Label1.Text);
doc.Load(PathXML1);
// string a1 = "bbsJZHY.aspx?x=200118211413";
string a2 = "//item[starts-with(floor,'" + TextBox2.Text + "')]";
alanfoxye 2008-03-18
  • 打赏
  • 举报
回复
程序是把Config文件读入内存的

修改文件中的value值然后再读System.Configuration.ConfigurationSettings.AppSettings["NetSelect"]不会读到修改后的内容
SeaverDing 2008-03-18
  • 打赏
  • 举报
回复
方法是没有问题的,只是括号中的参数好像不正确,如果出现你所描述的问题,检查一下修改Value值后有没有保存,然后将程序重新执行看一下。

110,534

社区成员

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

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

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