C#读取Xml文件

开始初步接触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函数可以用,我没有试过,所以不乱写了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值