一段读取xml配置文件属性的代码:(当前后有空行和注释时,也可安全读取):
string mCenter;
mCenter = ClientMain.get().ClientFilePathMgr.getConfigMediaPath() + "/PsCenter.xml";
//-------------------------------------------------------------------------
void _getContent()
{
XmlDocument file = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;//忽略注释
XmlReader reader = XmlReader.Create(mCenter, settings);
file.Load(reader);
XmlNodeList node_list = file.SelectSingleNode("Center").ChildNodes;
foreach (XmlElement i in node_list)
{
mIP = i.GetAttribute("Ip");
mPort = i.GetAttribute("Port");
mProjectName = i.GetAttribute("ProjectName");
mSoa = int.Parse(i.GetAttribute("SOA")) == 1 ? true : false;
}
}
原始xml:
<Center>
<!--CenterInfo ProjectName="Name" Ip="127.0.0.1" Port="5880" SOA="0" /-->
</Center>