• XML操作


    读取:
    XmlDocument source = new XmlDocument();
       XmlNode objnode;
       int i;
       string title,href;
       //载入新闻列表的XML文件
       source.Load(Server.MapPath("contents.xml"));
       objnode=source.SelectSingleNode("topiclist");
       //生成新闻列表
       Response.Write("<Table border=1>") ;
       Response.Write("<tr>");
       Response.Write("<td>");
       Response.Write("标  题");
       Response.Write("</td>");
       Response.Write("</tr>");
       if(objnode.HasChildNodes)
       {
        for(i=0;i<objnode.ChildNodes.Count;i++)
        {

         Response.Write("<tr>");
         title=objnode.ChildNodes[i].ChildNodes[0].InnerText;
         Response.Write("<td><a href=");
         href=objnode.ChildNodes[i].ChildNodes[1].InnerText;
         Response.Write(href);
         Response.Write(" target=main>");
         Response.Write(title);
         Response.Write("</a>");
         Response.Write("</td>");
         Response.Write("</tr>")  ;
        }
       }
       Response.Write("</table>");

    //====================================================================================
    写入:
    if (File.Exists(Server.MapPath(TextBox1.Text +".xml")))
        Response.Write("文件名已经存在,请重新选文件名!");
       else
       {
        //首先将数据插入新闻列表中
        XmlNode CurrNode;
        XmlDocument xmldoc = new XmlDocument();
        //加载索引文件
        xmldoc.Load(Server.MapPath("contents.xml"));
        string insstr;
        //生成新闻索引
        insstr="<topic><title>" +TextBox2.Text + "</title><href>main.aspx?name=" +TextBox1.Text + "</href></topic>";
        XmlDocumentFragment docFrag =xmldoc.CreateDocumentFragment();
        docFrag.InnerXml = insstr;
        CurrNode=xmldoc.DocumentElement;
        //插入新闻索引队列中
        CurrNode.InsertAfter(docFrag, CurrNode.LastChild);
        //保存索引文件
        xmldoc.Save (Server.MapPath("contents.xml"));


        //把TextBox5中的文件换成符合xml格式的内容
        string xmlfile;
        xmlfile=TextBox5.Text;
        xmlfile=xmlfile.Replace("<","+lt; ");
        xmlfile=xmlfile.Replace(">","+gt; ") ;
        xmlfile=xmlfile.Replace("//","+apos;") ;
        xmlfile=xmlfile.Replace ("\r\n","</paragraph><paragraph>");
        //把数据写入新建的xml文件中去;
        XmlDocument doc;
        doc=new XmlDocument() ;
        insstr="<?xml version='1.0' encoding='GB2312'?><document><title>" ;
        insstr +=TextBox2.Text  +"</title><abstract>" + TextBox4.Text ;
        insstr +="</abstract><author>" +TextBox3.Text +"</author><content><paragraph>";
        insstr +=xmlfile +"</paragraph></content></document>";
        doc.LoadXml(insstr) ;
        doc.Save (Server.MapPath(TextBox1.Text +".xml")) ;
        
       }
    //===========================================================================================
    CONTENTS.XML文件:
    <?xml version="1.0" encoding="GB2312"?>
    <topiclist type="Dreamsite News">
      <topic>
        <title>欢迎进入星球新闻网!</title>
        <href>main.aspx?name=hello</href>
      </topic>
      <topic>
        <title>测试新闻</title>
        <href>main.aspx?name=test</href>
      </topic>
      <topic>
        <title> 第一条新闻</title>
        <href>main.aspx?name=first</href>
      </topic>
      <topic>
        <title> 第二条新闻</title>
        <href>main.aspx?name=second</href>
      </topic>
      <topic>
        <title> 第三条新闻</title>
        <href>main.aspx?name=third</href>
      </topic>
      <topic>
        <title>第四条新闻</title>
        <href>main.aspx?name=fourth</href>
      </topic>
      <topic>
        <title>第五条新闻</title>
        <href>main.aspx?name=fifth</href>
      </topic>
      <topic>
        <title>第八条新闻</title>
        <href>main.aspx?name=eighth</href>
      </topic>
      <topic>
        <title>第八条新闻</title>
        <href>main.aspx?name=eighth</href>
      </topic>
      <topic>
        <title>第八条新闻</title>
        <href>main.aspx?name=eighth</href>
      </topic>
      <topic>
        <title>最后一条新闻</title>
        <href>main.aspx?name=last</href>
      </topic>
      <topic>
        <title>大风天气</title>
        <href>main.aspx?name=first</href>
      </topic>
      <topic>
        <title>大风</title>
        <href>main.aspx?name=first</href>
      </topic>
      <topic>
        <title>sss</title>
        <href>main.aspx?name=my.xml</href>
      </topic>
      <topic>
        <title>sss</title>
        <href>main.aspx?name=my.xml</href>
      </topic>
      <topic>
        <title>dfsf</title>
        <href>main.aspx?name=aa</href>
      </topic>
      <topic>
        <title>a</title>
        <href>main.aspx?name=a</href>
      </topic>
      <topic>
        <title>a</title>
        <href>main.aspx?name=a</href>
      </topic>
      <topic>
        <title>a</title>
        <href>main.aspx?name=a</href>
      </topic>
      <topic>
        <title>a</title>
        <href>main.aspx?name=a</href>
      </topic>
      <topic>
        <title>大风</title>
        <href>main.aspx?name=first</href>
      </topic>
      <topic>
        <title>太累</title>
        <href>main.aspx?name=mynews</href>
      </topic>
    </topiclist>

    //=========================================================================================
    private void Page_Load(object sender, System.EventArgs e)
      {
       ArrayList ar =new ArrayList();
       //获取当前目录的路径
       string dr =Server.MapPath("./");
       //获取当前目录下的所有XML文件
       string[] fils =Directory.GetFiles(dr,"*.xml");
       string Filename;
       int pos ;
       pos  = dr.Length+1;
       //显示当前目录下的新闻文件
       for(int i=0;i<fils.Length;i++)
       {
        Filename=Path.GetFileName(fils[i]);
        if( Filename.ToLower()!="contents.xml" )
         ar.Add(Filename);
       }
       dg.DataSource=ar;
       dg.DataBind();
      }

    private void submit_Click(object sender, System.EventArgs e)
      {
       if(Page.IsValid)
       {
        XmlDocument Xdoc = new XmlDocument();
        XmlNode objnode;
        string path,xmlfile;
        path=fname.Text;
        Xdoc.Load(path);
        //替换节点内容
        objnode=Xdoc.SelectSingleNode("document");
        objnode.ChildNodes[0].InnerText=title.Text;
        objnode.ChildNodes[2].InnerText=author.Text;
        objnode.ChildNodes[1].InnerText=abs.Text;
        xmlfile=cont.Text;
        //转换新闻内容格式
        xmlfile=xmlfile.Replace("<","&lt; ");
        xmlfile=xmlfile.Replace(">","&gt; ") ;
        xmlfile=xmlfile.Replace ("\r\n","</paragraph><paragraph>");
        xmlfile="<paragraph>" + xmlfile;
        xmlfile=xmlfile + "</paragraph>";
        objnode.ChildNodes[3].InnerText=xmlfile;
        Xdoc.Save(path);
        dis.Visible=false;
        Response.Write("修改成功!");
       }
      }

      private void dg_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
      {
       if( e.CommandName=="update")
       {
        string href,path,content;
        XmlDocument Xdoc = new XmlDocument();
        XmlNode objnode;
        href=e.Item.Cells[1].Text;
        path=Server.MapPath(href);
        Xdoc.Load(path);
        try
        {
         //存储文件名
         fname.Text=path;
         //获取各个节点的值
         objnode=Xdoc.SelectSingleNode("document");
         title.Text=objnode.ChildNodes[0].InnerText;
         author.Text=objnode.ChildNodes[2].InnerText;
         abs.Text=objnode.ChildNodes[1].InnerText;
         content=objnode.ChildNodes[3].InnerText;
         //替换XML格式
         content=content.Replace("&lt; ","<");
         content=content.Replace("&gt; ",">");
         content=content.Replace("</paragraph><paragraph>","\r\n");
         content=content.Replace("</paragraph>","");
         content=content.Replace("<paragraph>","");
         cont.Text=content;
         dis.Visible=true;
        }
        catch( Exception ee)
        {
         Response.Write(ee.ToString());
        }
       }
      }

    //=========================================================================================
    Hello.xml文件:
    <?xml version="1.0" encoding="GB2312"?>
    <document>
      <title>欢迎进入星球新闻网!</title>
      <abstract>Welcome</abstract>
      <author>shaozhd111</author>
      <content>&lt;paragraph&gt;首先欢迎您进入星球新闻网!&lt;/paragraph&gt;&lt;paragraph&gt;再次欢迎您进入星球新闻网!&lt;/paragraph&gt;</content>
    </document>

  • 相关阅读:
    审 讯 技巧
    带参数的多线程的方式
    通达信日线 数据格式
    visual studio 2012 update3
    单实例运行tz
    维特比算法
    Kooboo CMS的安装步骤
    年龄
    富文本编辑器---非常实用的
    printf 打印 unit32_t
  • 原文地址:https://www.cnblogs.com/RobotTech/p/534864.html
Copyright © 2020-2023  润新知