zoukankan      html  css  js  c++  java
  • C# 操作XML文件

    SysSet.xml
     1<?xml version="1.0" encoding="utf-8"?>
     2<SysNode>
     3  <SysInfo Type="File">
     4    <FileType>exe,com,cmd,dll,lnk</FileType>
     5    <FileNumber>5</FileNumber>
     6    <FileSize>20</FileSize>
     7  </SysInfo>
     8  <SysInfo Type="AutoSave">
     9    <Interval>10</Interval>
    10  </SysInfo>
    11  <SysInfo Type="FileBackUp">
    12    <DBBackFileUrl>E:\databake</DBBackFileUrl>
    13    <WebSiteDirectory>E:\databake\ESFM.WebNoTice532</WebSiteDirectory>
    14  </SysInfo>
    15  <SysInfo Type="SMS_on-off">
    16    <Value>0</Value>
    17  </SysInfo>
    18</SysNode>
    操作代码:
     1string filePath = Server.MapPath(".")+ "\\SysSet.xml";
     2            XmlDocument xd = new XmlDocument();
     3            xd.Load(filePath);
     4            XmlNodeList nodeList = xd.SelectSingleNode("SysNode").ChildNodes;
     5            foreach(XmlNode xn in nodeList)
     6            {
     7                if(xn.Attributes["Type"].Value == "File")
     8                {
     9                    this.AttType.Value = xn.SelectSingleNode("FileType").InnerText;
    10                    this.AttNum.Value = xn.SelectSingleNode("FileNumber").InnerText;
    11                    this.AttSize.Value = xn.SelectSingleNode("FileSize").InnerText;
    12                }

    13                else if (xn.Attributes["Type"].Value == "AutoSave")
    14                {
    15                    this.AutoTime.Value = xn.SelectSingleNode("Interval").InnerText; 
    16                }

    17                else if (xn.Attributes["Type"].Value == "FileBackUp")
    18                {
    19                    this.DBBackUrl.Value = xn.SelectSingleNode("DBBackFileUrl").InnerText;
    20                    this.FileBackDir.Value = xn.SelectSingleNode("WebSiteDirectory").InnerText;
    21                }

    22                else if (xn.Attributes["Type"].Value == "SMS_on-off")
    23                {
    24                    if (xn.SelectSingleNode("Value").InnerText == "0")
    25                    {
    26                        this.btnOnOff.Text = "开通";
    27                    }

    28                    else this.btnOnOff.Text = "关闭";
    29                }

    30            }


    我现在想用c#的form中的几个textbox写入数据到xml中,然后在读出来~  
      代码如下:  
      1。写入xml文件。  
      XmlTextWriter   write=new   XmlTextWriter("Filesheet.xml",null);  
      write.Formatting=Formatting.Indented;  
      write.WriteStartDocument();  
      write.WriteStartElement("Filesheet");  
      write.WriteAttributeString("xmlns",null,"http://tempuri.org/Filesheet.xsd");  
      write.WriteStartElement("fsite",null);  
      write.WriteAttributeString("flable",txtsite.Text);  
      write.WriteEndAttribute();  
      write.WriteAttributeString("fzhuji",txtzhuji.Text);  
      write.WriteEndAttribute();  
      write.WriteAttributeString("fuser",txtuser.Text);  
      write.WriteEndAttribute();  
      write.WriteAttributeString("fpassword",txtpassword.Text);  
      write.WriteEndAttribute();  
      write.WriteAttributeString("fdankou",txtdankou.Text);  
      write.WriteEndAttribute();  
      write.WriteAttributeString("fzhushi",txtzhushi.Text);  
      write.WriteEndAttribute();  
      write.WriteEndElement();  
      write.Flush();  
      write.WriteEndDocument();  
      write.Close(); 问题点数:30、回复次数:10Top

    照片处理软件下载

    1 楼zxphf( 海 风)回复于 2005-04-01 09:38:22 得分 0

    2。读取xml  
      XmlDocument   doc=new   XmlDocument();  
      doc.Load("Filesheet.xml");  
      XmlNode   node=doc.DocumentElement.FirstChild;  
      //XmlNamedNodeMap   attrs=node.Attributes;  
      XmlAttributeCollection   attrs=node.Attributes;  
      txtsite.Text=attrs[0].Value;  
      txtzhuji.Text=attrs[1].Value;  
      txtuser.Text=attrs[2].Value;  
      txtpassword.Text=attrs[3].Value;  
      txtzhushi.Text=attrs[4].Value;Top

    2 楼zxphf( 海 风)回复于 2005-04-01 09:40:29 得分 0

    现在遇到问题,我的文档只能写一条记录~有没有谁有更好的控制和方法~最好给我要实现的这样的代码。欢迎强人来解答啊~Top

    3 楼zxphf( 海 风)回复于 2005-04-01 09:41:21 得分 0

    3。还有这个  
      <?xml   version="1.0"   encoding="utf-8"   ?>  
      <xsd:schema   xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
      <xsd:element   name="fsite">  
      <xsd:complexType>  
      <xsd:attribute   name="flable"   type="xsd:string"   />  
      <xsd:attribute   name="fzhuji"   type="xsd:string"   />  
      <xsd:attribute   name="fuser"   type="xsd:string"   />  
      <xsd:attribute   name="fpassword"   type="xsd:string"   />  
      <xsd:attribute   name="fduankou"   type="xsd:int"   />  
      <xsd:attribute   name="fzhushi"   type="xsd:string"   />  
      </xsd:complexType>  
      </xsd:element>  
      </xsd:schema>Top

    4 楼littlekeen(keen)回复于 2005-04-01 09:54:11 得分 15

    读取xml:例子里是直接在页面显示出来  
      public   class   readXml   :   System.Web.UI.Page  
      {  
      private   const   string   filename   =   "d:\\aa.xml";  
      private   void   Page_Load(object   sender,   System.EventArgs   e)  
      {  
      XmlTextReader   reader   =   null;  
       
      try    
      {  
                     
      //   Load   the   reader   with   the   data   file   and   ignore   all   white   space   nodes.                    
      reader   =   new   XmlTextReader(filename);  
      reader.WhitespaceHandling   =   WhitespaceHandling.None;  
       
      //   Parse   the   file   and   display   each   of   the   nodes.  
      while   (reader.Read())    
      {  
      switch   (reader.NodeType)    
      {  
      case   XmlNodeType.Element:  
      Response.Write(   reader.Name);  
      break;  
      case   XmlNodeType.Text:  
      Response.Write(reader.Value);  
      break;  
      case   XmlNodeType.CDATA:  
      Response.Write(reader.Value);  
      break;  
      case   XmlNodeType.ProcessingInstruction:  
      Response.Write(   reader.Name+   reader.Value);  
      break;  
      case   XmlNodeType.Comment:  
      Response.Write(reader.Value);  
      break;  
      case   XmlNodeType.XmlDeclaration:  
      Response.Write("<?xml   version='1.0'?>");  
      break;  
      case   XmlNodeType.Document:  
      break;  
      case   XmlNodeType.DocumentType:  
      Response.Write(   reader.Name+   reader.Value);  
      break;  
      case   XmlNodeType.EntityReference:  
      Response.Write(reader.Name);  
      break;  
      case   XmlNodeType.EndElement:  
      Response.Write(   reader.Name);  
      break;  
      }                
      }                        
      }  
       
      finally    
      {  
      if   (reader!=null)  
      reader.Close();  
      }  
       
      }Top

    5 楼zxphf( 海 风)回复于 2005-04-01 09:59:28 得分 0

    我怎么感觉不怎么和我的很相同啊。。?  
      我现在想用c#的form中的几个textbox写入数据到xml中,然后在读出来~  
      写的过程我只能写一条记录,再写的时候他就覆盖第一条记录的。上面的代码只是为了更好的说明的  
      不过还是很感谢你的~  
      Top

    6 楼qozms(博德之门)回复于 2005-04-01 10:06:17 得分 10

    在C#中操作XML    
       
       
      我用的是一种很笨的方法,但可以帮助初学者了解访问XML节点的过程。  
         
          已知有一个XML文件(bookstore.xml)如下:  
       
       
        <?xml   version="1.0"   encoding="gb2312"?>  
      <bookstore>  
          <book   genre="fantasy"   ISBN="2-3631-4">  
              <title>Oberon's   Legacy</title>  
              <author>Corets,   Eva</author>  
              <price>5.95</price>  
          </book>  
      </bookstore>  
       
         
       
       
         
          1、往<bookstore>节点中插入一个<book>节点:  
       
       
       
       
            XmlDocument   xmlDoc=new   XmlDocument();  
            xmlDoc.Load("bookstore.xml");  
            XmlNode   root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>  
            XmlElement   xe1=xmlDoc.CreateElement("book");//创建一个<book>节点  
            xe1.SetAttribute("genre","李赞红");//设置该节点genre属性  
            xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性  
         
            XmlElement   xesub1=xmlDoc.CreateElement("title");  
            xesub1.InnerText="CS从入门到精通";//设置文本节点  
            xe1.AppendChild(xesub1);//添加到<book>节点中  
             
            XmlElement   xesub2=xmlDoc.CreateElement("author");  
            xesub2.InnerText="候捷";  
            xe1.AppendChild(xesub2);  
             
            XmlElement   xesub3=xmlDoc.CreateElement("price");  
            xesub3.InnerText="58.3";  
            xe1.AppendChild(xesub3);  
         
            root.AppendChild(xe1);//添加到<bookstore>节点中  
            xmlDoc.Save("bookstore.xml");  
       
         
       
       
       
          //================  
          结果为:  
       
       
       
       
        <?xml   version="1.0"   encoding="gb2312"?>  
      <bookstore>  
          <book   genre="fantasy"   ISBN="2-3631-4">  
              <title>Oberon's   Legacy</title>  
              <author>Corets,   Eva</author>  
              <price>5.95</price>  
          </book>  
          <book   genre="李赞红"   ISBN="2-3631-4">  
              <title>CS从入门到精通</title>  
              <author>候捷</author>  
              <price>58.3</price>  
          </book>  
      </bookstore>  
       
         
       
       
       
      2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。  
       
       
                XmlNodeList   nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点  
            foreach(XmlNode   xn   in   nodeList)//遍历所有子节点  
            {  
              XmlElement   xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型  
              if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”  
              {  
                xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”  
         
                XmlNodeList   nls=xe.ChildNodes;//继续获取xe子节点的所有子节点  
                foreach(XmlNode   xn1   in   nls)//遍历  
                {  
                  XmlElement   xe2=(XmlElement)xn1;//转换类型  
                  if(xe2.Name=="author")//如果找到   
                  {  
                    xe2.InnerText="亚胜";//则修改  
                    break;//找到退出来就可以了  
                  }  
                }  
                break;  
              }  
            }  
         
            xmlDoc.Save("bookstore.xml");//保存。  
       
         
       
       
          //=================  
       
          最后结果为:  
       
       
        <?xml   version="1.0"   encoding="gb2312"?>  
      <bookstore>  
          <book   genre="fantasy"   ISBN="2-3631-4">  
              <title>Oberon's   Legacy</title>  
              <author>Corets,   Eva</author>  
              <price>5.95</price>  
          </book>  
          <book   genre="update李赞红"   ISBN="2-3631-4">  
              <title>CS从入门到精通</title>  
              <author>亚胜</author>  
              <price>58.3</price>  
          </book>  
      </bookstore>  
       
         
       
         
          3、删除   <book   genre="fantasy"   ISBN="2-3631-4">节点的genre属性,删除   <book   genre="update李赞红"   ISBN="2-3631-4">节点。  
       
       
        XmlNodeList   xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;  
         
            foreach(XmlNode   xn   in   xnl)  
            {  
              XmlElement   xe=(XmlElement)xn;  
         
       
            if(xe.GetAttribute("genre")=="fantasy")  
              {  
                xe.RemoveAttribute("genre");//删除genre属性  
              }  
              else   if(xe.GetAttribute("genre")=="update李赞红")  
              {  
                xe.RemoveAll();//删除该节点的全部内容  
              }  
            }  
            xmlDoc.Save("bookstore.xml");  
       
          //====================  
           
          最后结果为:  
       
       
        <?xml   version="1.0"   encoding="gb2312"?>  
      <bookstore>  
          <book   ISBN="2-3631-4">  
              <title>Oberon's   Legacy</title>  
              <author>Corets,   Eva</author>  
              <price>5.95</price>  
          </book>  
          <book>  
          </book>  
      </bookstore>    
       
         
          4、显示所有数据。  
       
       
          XmlNode   xn=xmlDoc.SelectSingleNode("bookstore");  
         
            XmlNodeList   xnl=xn.ChildNodes;  
             
            foreach(XmlNode   xnf   in   xnl)  
            {  
              XmlElement   xe=(XmlElement)xnf;  
              Console.WriteLine(xe.GetAttribute("genre"));//显示属性值  
              Console.WriteLine(xe.GetAttribute("ISBN"));  
         
              XmlNodeList   xnf1=xe.ChildNodes;  
              foreach(XmlNode   xn2   in   xnf1)  
              {  
                Console.WriteLine(xn2.InnerText);//显示子节点点文本  
              }  
            }    
         
       
       
       
       
      转载自:http://blog.csdn.net/s98/archive/2004/10/17/140307.aspx  
         
       
       
         
       
       
       
         
       
       
      版权声明:是本Blog托管服务提供商。如本文牵涉版权问题,不承担相关责任,请版权拥有者直接与文章作者联系解决。  
       
      [点击此处收藏本文]    
      发表于   2004年10月17日   11:48   PM    
       
       
      评论  
      #   回复:在C#中操作XML   2005-03-01   9:02   AM   momo    
      xmlDoc.load(request)=true    
       
      这句话表示什么意思?    
       
       
       
      发表评论  
      标题:      
      请输入标题      
      大名:      
      请输入尊姓大名      
      网址:        
      评论      
      请输入评论    
           
              记住我?    
         
       
      Powered   by:    
           
      Copyright   ?   Johnny    
      Top

    7 楼zxphf( 海 风)回复于 2005-04-01 10:24:09 得分 0

    能不能看看我的问题在发啊  
      我现在是想:为什么写进去的数据总覆盖第一条,那样我的文件里面老只有一条记录的~Top

    8 楼zxphf( 海 风)回复于 2005-04-01 11:09:43 得分 0

    没人知道吗?我想应该有高手在的啊  
      高手门,拜托拉,这个问题我已经搞了好多天了啊~  
      不知道帮忙顶一下啊Top

    9 楼sj_breezerain(breeze-rain)回复于 2005-04-01 14:40:54 得分 5

    qozms(红翻天)   的写法不会覆盖第一条的啊!Top

  • 相关阅读:
    ASP'禁止网页缓存,验证码生成,删除文件 iFileName 文件名 iPath 文件路径
    ASP.NET中如何防范SQL注入式攻击
    ThickBox 2.0
    ASP.NET用户控件也可以在Web.Config中进行引用
    SQL Server数据类型介绍
    CSS和JS标签style属性对照表
    Server.UrlEncode、HttpUtility.UrlDecode的区别[两篇文章]
    使用table数据类型变量获得临时表
    关闭页面JS兼容火狐_谷歌_IE6、7、8
    SQL语句实现两个数据库表直接操作
  • 原文地址:https://www.cnblogs.com/gfwei/p/995417.html
Copyright © 2011-2022 走看看