2 回答

TA貢獻1797條經驗 獲得超6個贊
1)修改一下XML文件,去掉<?xml-stylesheet ……/>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8" ?> <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> |
2)在程序中,讀取XML (假設XML文件名為acc.xml)
1 2 3 4 | using System.Data; …… DataSet ds = new DataSet(); ds.ReadXml("acc.xml"); |
3)當用戶輸入的用戶userId和口令userPwd后,在ds中查找并驗證
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | DataRow[] drs = ds.Tables[0].Select("id='" + userId + "'"); if(drs.Length== 0) { //XML文件中沒有用戶輸入的userId } else { DataRow r = drs[0]; //檢查口令是否匹配 if( r["password"] == userPwd) { //口令正確 } else { //口令錯誤 } } |
4)其他操作:給定id,取出年齡、性別
1 2 3 4 5 6 7 | DataRow[] drs = ds.Tables[0].Select("id='" + id + "'"); if(drs.Length== 0) return; DataRow r = drs[0]; //性別 string gendar = r["xingbie"].ToString(); //年齡 int age = nt.Parse(r["age"].ToString()); |

TA貢獻1757條經驗 獲得超7個贊
解析xml還是比較簡單的,遍歷XmlDocument.DocumentElement.ChildNodes,每個XmlElement都有SelectSingleNode方法,通過該方法可以獲取InnerText,比如element.SelectSingleNode("password").InnerText,然后用這個值和輸入進行判斷。具體請查找XmlDocument類。
- 2 回答
- 0 關注
- 1567 瀏覽
添加回答
舉報