新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C#.NET示例读写xml所有节点的代码实现方法和如何读取xml节点的数据总结 - XML标记语言基础、XML技术应用 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5826 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C#.NET示例读写xml所有节点的代码实现方法和如何读取xml节点的数据总结 - XML标记语言基础、XML技术应用 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 C#.NET示例读写xml所有节点的代码实现方法和如何读取xml节点的数据总结 - XML标记语言基础、XML技术应用

    全文 -

    C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结


    读:
        //打开某文件(假设web.config在根目录中)
        string filename=Server.MapPath("/") + @"WebApplication1\web.config";
        XmlDocument xmldoc= new XmlDocument();
        xmldoc.Load(filename);

        //得到顶层节点列表
        XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
        foreach(XmlElement element in topM)
        {
         if(element.Name.ToLower()=="appsettings")
         {

          //得到该节点的子节点
          XmlNodeList nodelist=element.ChildNodes;

          if ( nodelist.Count >0 )
          {
           //DropDownList1.Items.Clear();

           foreach(XmlElement el in nodelist)//读元素值
           {
            //DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
            //this.TextBox2.Text=el.Attributes["key"].InnerText;
            this.TextBox2.Text=el.Attributes["key"].Value;
            this.Label1.Text=el.Attributes["value"].Value;


                //同样在这里可以修改元素值,在后面save。
             //  el.Attributes["value"].Value=this.TextBox2.Text;
           }

          }

         }

        }

        xmldoc.Save(filename);

       }

    效果:
      <appSettings>
        <add key="密码" value="admin" />
        <add key="ltp" value="first" />
      </appSettings>

    //添加空元素:
       XmlNode node=doc.CreateElement(groupname);
         node.InnerText="";
         doc.LastChild.AppendChild(node);

         doc.Save(xmlfile);

    删除一个节点元素
       string itemname=this.listBox1.SelectedItem.ToString();
       
       this.listBox1.Items.Remove(this.listBox1.SelectedItem);

       //begin del xmlfile
       XmlDocument doc=new XmlDocument();
       doc.Load(xmlfile);
       
       XmlNodeList topM=doc.DocumentElement.ChildNodes;
       foreach(XmlElement element in topM)
       {
        if(element.Name==this.comboBox1.Text)
        {

         //得到该节点的子节点
         XmlNodeList nodelist=element.ChildNodes;      

         foreach(XmlElement el in nodelist)//读元素值
         {       
          if(el.Attributes["key"].Value==itemname)

    {
           element.RemoveChild(el);
          }

         }//循环元素
          
        }//得到组

       }//循环组

       doc.Save(xmlfile);  //一定要保存一下,否则不起作用

    //筛选数据
    private void Reader_Xml(string pathFlie)
    {
       XmlDocument Xmldoc=new XmlDocument();
       Xmldoc.Load(pathFlie);
       XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Code[@id='1'])
       int f=0;
       foreach(XmlNode xnode in Record1)
        {

        }
    } /**//*读取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>  


    在某节点下增加一个元素,并设置值:

        if(element.Name.ToLower()=="appsettings")
        {

         XmlElement elem =xmldoc.CreateElement("add");
         
         element.AppendChild(elem);
         elem.InnerText="ltp";

         xmldoc.Save(filename);
           
        }

    效果:
      <appSettings>
        <add key="密码" value="admin" />
        <add>ltp</add>
      </appSettings>


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/8/10 9:58:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客2
    发贴心情 
    在某节点下增加一个元素,并增加两个属性:
        if(element.Name.ToLower()=="appsettings")
        {

         XmlElement elem =xmldoc.CreateElement("add");
         element.AppendChild(elem);

         XmlAttribute xa=xmldoc.CreateAttribute("key");
         xa.Value="ltp";

         XmlAttribute xa2=xmldoc.CreateAttribute("value");
         xa2.Value="first";

         elem.SetAttributeNode(xa);
         elem.SetAttributeNode(xa2);

         xmldoc.Save(filename);

    </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;
        }

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/8/10 10:03:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/3 0:23:20

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    5,721.069ms