XML 文件做配置文件,返回参数列表方式 请教
//遍历xml文件的节点信息
private static void GetXmlNodeInformation(string xmlPath)
{
try
{
//定义并从xml文件中加载节点(根节点)
XElement rootNode = XElement.Load(xmlPath);
//查询语句: 获得根节点下name子节点(此时的子节点可以跨层次:孙节点、重孙节点......)
IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("port")
select target;
foreach (XElement node in targetNodes)
{
Console.WriteLine("port = {0}", node.Value);
}
//查询语句: 获取ID属性值等于"111111"并且函数子节点的所有User节点(并列条件用"&&"符号连接)
IEnumerable<XElement> myTargetNodes = from myTarget in rootNode.Descendants("text")
where myTarget.Attribute("ID").Value.Equals("1")
&& myTarget.HasElements
select myTarget;
foreach (XElement node in myTargetNodes)
{
Console.WriteLine("text = {0}", node.Element("ascii").Value);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
<?xml version="1.0" encoding="utf-8"?>
<configs>
<host ID="1">
<port>60000</port>
</host>
<text ID="1">
<ascii>0</ascii>
</text>
</configs>
<?xml version="1.0" encoding="utf-8" ?>我是这样设计的,你借鉴一下
- <root>
<db constr="User ID=root;Password=root;Host=localhost;Port=3306;Database=test080121nb2014;Protocol=TCP;Compress=false;Pooling=true;Min Pool Size=3;Max Pool Size=100;CharSet=utf8;Connect Timeout=30000; RespectBinaryFlags=false;TreatTinyAsBoolean=false" />
<bar printer="BAR" />
<kitchen printer="BAR" />
<orders printer="yan" />
<package timeout="5000" />
<timeserver serverip="192.168.0.1" serverport="123" sleeptime="1800000" />
<log maxline="300000" />
</root>