[摘抄]xml字符串的一次性解析,

xml字符串的解析,有的可能喜欢用反序列化的方式。但是对于只是解析一次这个字符串,显得没有必要。
比如下面这个字符串。

string xml = "<Bossien>"
+ "<head>"
+ "<PassWord>123456789</PassWord>"
+ "<ServiceCode>ESTUDY001</ServiceCode>"
+ "</head>"
+ "<body>"
+ "<PeopleInfo>"
+ "<RegType>安管</RegType>"
+ "<IDCard>3203231993052802**</IDCard>"
+ "<RealName>陈生</RealName>"
+ "<Sex>男</Sex>"
+ "<Mobile>130063971**</Mobile>"
+ "<Company>易思达</Company>"
+ "<Education>研究生及以上</Education>"
+ "<Unit>省属生产经营单位</Unit>"
+ "<CompanyType>非煤矿山生产经营单位</CompanyType>"
+ "<TrainingType>初训</TrainingType>"
+ "<QualificaID>0001|000101|00010103</QualificaID>"
+ "<Agency>234</Agency>"
+ "</PeopleInfo>"
+ "</body>"
+ "</Bossien>";

在bossien这个标签下分了head和body这两层。两层下又有子层。那么在解析的时候可用这样的方法。

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode xn = doc.SelectSingleNode("Bossien");
XmlNodeList xn1 = xn.ChildNodes;
string password = xn1.Item(0).SelectSingleNode("PassWord").InnerText;
string ServiceCode = xn1.Item(0).SelectSingleNode("ServiceCode").InnerText;
XmlNodeList xn2 = xn1.Item(1).SelectSingleNode("PeopleInfo").ChildNodes;
分别取
xn2.Item(0).InnerText;
xn2.Item(1).InnerText;
······

这里就有两种取子节点的方法了。
一种是SelectSingleNode();参数是子节点的名称。
第二种是ChildNodes,这个是获取所有子节点,通过Item(index)来获取。
---------------------
作者:波风亭
来源:CSDN
原文:https://blog.csdn.net/u014472643/article/details/62039600
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/leeke/p/11216668.html