开始初步接触Xml文件的读写,此处记录一下用法:
命名空间:
using System.Xml;
xml文件:用记事本编辑,保存时要注意编码方式选择utf-8,默认的ANSI格式读写时可能[*]报错。文件名为.\ClientLocal.xml。格式有些乱,对付着看吧
<?xml version="1.0" ?>
<configuration>
<listFile Name=".\FileList.xml" />
<clientAssembly Name=".\FrameWindow.exe" />
<moveTestDir Name=".\MoveTest\" />
<bufferDirForClientAssembly Name=".\FileBuffer\" />
<logFile Name=".\RunLog.txt" />
<noCheck Tip="Files or folders no need to update">
<item Type="File">ClientLocal.Xml</item>
<item Type="Folder">MoveTest</item>
<item Type="Folder">FileBuffer</item>
</noCheck>
<netSetting ClientIP="172.16.18.28" ServerIP="172.16.18.28" ServerPort="3437" />
</configuration>
读取方式:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(".\\ClientLocal.xml");//使用Load而不是LoadXml,可以读取ANSI编码的Xml,并且可以少一些莫名其妙的错误
XmlNode snode = xdoc.SelectSingleNode("/configuration/bufferDirForClientAssembly");
string bufferDir = snode.Attributes["Name"].Value;//读取属性值
string fileList = xdoc.SelectSingleNode("/configuration/listFile/@Name").Value;//可以直接用一句读出属性
snode=xdoc.SelectSingleNode("/configutation/noCheck/item");
string noCheckFile=snode.InnerText;//读取值
snode=xdoc.SelectSingleNode("/configuration/noCkeck/item[contains(text(),'Move') and contains(@Type,'Fold')]");//查找值包含Move和属性Type包含Fold的第一个节点
主要是Contains()的用法,当然还有直接比较大小的[item > 'Dll']等来选择性获取所需数据,另外还有一个match函数可以用,我没有试过,所以不乱写了。