zoukankan      html  css  js  c++  java
  • 读取xml节点的数据总结

    /*读取xml数据   两种xml方式*/
    <aaa>
         <bb>something</bb>
         <cc>something</cc>
    </aaa>
     
    <aaa>
        <add key="123" value="321"/>
    </aaa>

    /*第一种方法*/
    DS.ReadXml("your xmlfile name");
    Container.DataItem("bb");
    Container.DataItem("cc");
    DS.ReadXmlSchema("your xmlfile name");
     
    /*第二种方法*/
    <aaa>
        <add key="123" value="321"/>
    </aaa>
    如果我要找到123然后取到321应该怎么写呢?
     
    using System.XML;
    XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
    xmlDoc.Load(@"c:\Config.xml");
    XmlElement elem = xmlDoc.GetElementById("add");
    string str = elem.Attributes["value"].Value
     
     
    /*第三种方法:  SelectSingleNode  读取两种格式的xml *---/
    --------------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
           <ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>            
      </appSettings>
    </configuration>
    --------------------------------------------------------------------------
    XmlDocument doc = new XmlDocument();
    doc.Load(strXmlName);
     
        XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
        if(node!=null)
        {
         string k1=node.Value;    //null
         string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
         string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
         node=null;
        }
     
    ********************************************************************
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
           <add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />            
      </appSettings>
    </configuration>
    **--------------------------------------------------------------------**
         XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
        if(node!=null)
        {
         string k=node.Attributes["key"].Value;
         string v=node.Attributes["value"].Value;
         node=null;
        }
    *--------------------------------------------------------------------*
        XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
        if(node!=null)
        {
         XmlNodeReader nr=new XmlNodeReader(node);
         nr.MoveToContent();
        //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
         nr.MoveToAttribute("value");
         string s=nr.Value;
         node=null;
        }

  • 相关阅读:
    Flutter Icons 内置图标库,全套Material图标
    解决cannot connect to daemon at tcp:5037: cannot connect to 127.0.0.1:5037: 由于目标计算机积极拒绝,无法连接。 (10061).
    mavenCentral()、jcenter()、google()仓库
    flutter doctor检查出现多个Android Studio解决办法
    Oracle trunc 函数用法详解
    将博客搬至CSDN
    Yii2 高级模板不使用Apache配置目录,将前后台入口移到根目录
    报警告session_regenerate_id(): Failed to create(read) session ID: files (path: N;/path)
    yii2GridView的简单使用
    yii 表单如何写,action指向哪里?
  • 原文地址:https://www.cnblogs.com/ltp/p/289618.html
Copyright © 2011-2022 走看看