zoukankan      html  css  js  c++  java
  • xml的解析

       XmlDocument doc=new XmlDocument();
       StringReader sr=new StringReader(textBox1.Text);
       XmlTextReader rd=new XmlTextReader(sr);

       while(rd.Read())
       {
        //textBox2.Text +=rd.NodeType.ToString()+"\r\n";
        if(rd.NodeType.ToString()=="Element")
        {
         textBox2.Text +=rd.GetAttribute("name")+"\r\n";
        // textBox2.Text +=rd.AttributeCount.ToString()+"\r\n";
        // textBox2.Text +=rd.Value+"\r\n";
        }
       }

    用字符串初始化xml   
       XmlDocument xmldoc=new XmlDocument();
       xmldoc.LoadXml(textBox1.Text );

    xmldoc.ChildNode有两个,0是xml的标签,1是内容
    xmldoc["attributes"]  用名字引用node
    foreach(XmlNode xn in xmldoc["attributes"]) 遍历某个属性的子节点
    foreach(XmlNode xn2 in xn)  遍历某个节点的子节点
    xn.Attributes["AttrType"].Value  某个属性的值
    xn2.InnerText  叶节点中间的文字

    <?xml version="1.0" encoding="GB2312"?>
    <attributes>
      <attribute type='1' name='地址' value=''/>
      <attribute type='2' name='星级' value='一星'>
        <value>一星</value>
        <value>二星</value>
        <value>三星</value>
        <value>四星</value>
        <value>五星</value>
      </attribute>
      <attribute type='3' name='房间种类'>
        <value>单人间</value>
        <value>双人间</value>
        <value>总统套房</value> 
      </attribute>
    </attributes>

  • 相关阅读:
    Java中类与类的关系
    谈谈spring
    mybatis和hibernate的区别
    微信小程序文档解读(一)--api提供支持有哪些
    nodejs问题整理--fs.exists无法正确判断文件的问题
    微信小程序-多级联动
    react
    [微信小程序] 终于可以愉快的使用 async/await 啦
    [Node] 逃离回调地狱
    单例模式
  • 原文地址:https://www.cnblogs.com/jetz/p/201308.html
Copyright © 2011-2022 走看看