.NET中读取xml文件中数据

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="aaa.css"?>
<zhanghaos>
<zhanghao>
<id>zyw</id>
<password>123</password>
<xingbie>男</xingbie>
<age>12</age>
</zhanghao>
<zhanghao>
<id>asd</id>
<password>321</password>
<xingbie>女</xingbie>
<age>21</age>
</zhanghao>
</zhanghaos>
代码是这样。现在我想写一个登陆界面,需要读取数据然后与输入对比id和password,怎么弄?

HUX布斯
浏览 1544回答 2
2回答

互换的青春

1)修改一下XML文件,去掉<?xml-stylesheet ……/>123456789101112131415<?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"&nbsp;?><zhanghaos>&nbsp;&nbsp;<zhanghao>&nbsp;&nbsp;&nbsp;&nbsp;<id>zyw</id>&nbsp;&nbsp;&nbsp;&nbsp;<password>123</password>&nbsp;&nbsp;&nbsp;&nbsp;<xingbie>男</xingbie>&nbsp;&nbsp;&nbsp;&nbsp;<age>12</age>&nbsp;&nbsp;</zhanghao>&nbsp;&nbsp;<zhanghao>&nbsp;&nbsp;&nbsp;&nbsp;<id>asd</id>&nbsp;&nbsp;&nbsp;&nbsp;<password>321</password>&nbsp;&nbsp;&nbsp;&nbsp;<xingbie>女</xingbie>&nbsp;&nbsp;&nbsp;&nbsp;<age>21</age>&nbsp;&nbsp;</zhanghao></zhanghaos>2)在程序中,读取XML (假设XML文件名为acc.xml)1234using&nbsp;System.Data;……DataSet&nbsp;ds&nbsp;=&nbsp;new&nbsp;DataSet();ds.ReadXml("acc.xml");3)当用户输入的用户userId和口令userPwd后,在ds中查找并验证123456789101112131415161718DataRow[] drs = &nbsp; ds.Tables[0].Select("id='"&nbsp;+ userId +&nbsp;"'");if(drs.Length== 0){&nbsp;&nbsp; &nbsp;//XML文件中没有用户输入的userId}else{&nbsp;&nbsp; &nbsp;DataRow r &nbsp; = drs[0];&nbsp;&nbsp; &nbsp;//检查口令是否匹配&nbsp;&nbsp; &nbsp;if( &nbsp; r["password"] == userPwd)&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//口令正确&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;else&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//口令错误&nbsp;&nbsp; &nbsp;}}4)其他操作:给定id,取出年龄、性别1234567DataRow[]&nbsp;drs&nbsp;=&nbsp;ds.Tables[0].Select("id='"&nbsp;+&nbsp;id&nbsp;+&nbsp;"'");if(drs.Length==&nbsp;0)&nbsp;return;DataRow&nbsp;r&nbsp;=&nbsp;drs[0];//性别string&nbsp;gendar&nbsp;=&nbsp;r["xingbie"].ToString();//年龄int&nbsp;age&nbsp;=&nbsp;nt.Parse(r["age"].ToString());&nbsp;

长风秋雁

解析xml还是比较简单的,遍历XmlDocument.DocumentElement.ChildNodes,每个XmlElement都有SelectSingleNode方法,通过该方法可以获取InnerText,比如element.SelectSingleNode("password").InnerText,然后用这个值和输入进行判断。具体请查找XmlDocument类。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5