用XmlReader 取xml文件节点的值
在项目中,同一个配置在不同的目录下要有不同的值,而目录又是不确定的,这时就需要将配置信息存放在相应的目录中,在运行时根据路径去取
方法:用xml文件存储,放在使用目录下,用下面方法获取配置信息
public class yzzConfig
{
/// <summary>
/// 获取Xml文件配置信息
/// </summary>
/// <param name="node">节点名</param>
/// <param name="path">文件路径</param>
/// <returns></returns>
public static string AppSettings(string node, string path)
{
string result = string.Empty;
try
{
//XmlReaderSettings settings = new XmlReaderSettings();
//settings.IgnoreComments = true;
//settings.IgnoreProcessingInstructions = true;
//settings.IgnoreWhitespace = true;
using (XmlReader reader = XmlReader.Create(path))//, settings))
{
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == node)
{
result = reader.ReadInnerXml();
break;
}
}
reader.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}
}
XML文件示例
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<yzzSettings>
<xmlxslt>test</xmlxslt>
</yzzSettings>
<configSections>
调用示例
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(yzzConfig.AppSettings("xmlxslt", string.Format("{0}\\yzzconfig.xml",
Request.Ph
相关文档:
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
......
xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
<wap2>false</wa ......
今天才知道CMarkup可以直接解析字符串形式的XML。以前都是先存入一个文件,然后从文件中load。多做了I/O操作,效率不高。
CMarkup xml;
CString str;
xml.SetDoc(str);
tinyXml也可以直接解析XML字符串,方式如下:
// directly parsing string with tinyxml
const char* ......
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/bea ......