zoukankan      html  css  js  c++  java
  • [摘抄]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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    centos ssh远程登陆
    Vim 技巧
    php and js to facebook登陆 最佳实践
    vim 编辑器常规操作
    js 函数定义三种方式
    ORDER BY 默认升序排列
    sql = 和<>遵循的sql-92标准的设置SET ANSI_NULLS ON
    添加FB登陆时,需要curl扩展
    array(1) { [0]=> int(5) }和array(1) { [0]=> string(1) "5" }
    Android第三方推送引擎比较
  • 原文地址:https://www.cnblogs.com/leeke/p/11216668.html
Copyright © 2011-2022 走看看