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

    using System;
      2  using System.Collections;
      3using System.Configuration;
      4using System.Data;
      5using System.Linq;
      6using System.Web;
      7using System.Web.Security;
      8using System.Web.UI;
      9using System.Web.UI.HtmlControls;
     10using System.Web.UI.WebControls;
     11using System.Web.UI.WebControls.WebParts;
     12using System.Xml.Linq;
     13using System.Data.SqlClient;
     14using System.Xml;
     15
     16public partial class OperateXmlFile : System.Web.UI.Page
     17{
     18    SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ForTestConn"].ConnectionString);
     19    protected void Page_Load(object sender, EventArgs e)
     20    {
     21        
     22    }

     23
     24    protected void WriteXML()
     25    {
     26        string strSQL = "SELECT TOP 5 * FROM TestUser";
     27
     28        SqlDataAdapter sqlSDR = new SqlDataAdapter(strSQL, sqlConn);
     29        DataSet ds = new DataSet();
     30        sqlSDR.Fill(ds, "user");
     31
     32        //ReadXml方法
     33        //ds.ReadXml(Server.MapPath("XMLFile.xml"));
     34
     35        //this.GridView1.DataSource = ds;
     36        //this.GridView1.DataBind();
     37
     38        //GetXml方法
     39        //Response.Write(ds.GetXml());
     40
     41        //WriteXml方法
     42        //ds.WriteXml(Server.MapPath("XMLFile.xml"));
     43
     44        XmlDocument xmlDoc = new XmlDocument();
     45        xmlDoc.Load(Server.MapPath("XMLFile.xml"));
     46
     47        XmlNode root = xmlDoc.SelectSingleNode("users");
     48
     49        foreach (DataRow row in ds.Tables[0].Rows)
     50        {
     51            XmlElement xeUser = xmlDoc.CreateElement("user");
     52
     53            XmlElement xeUserName = xmlDoc.CreateElement("UserName");
     54            xeUserName.InnerText = row["UserName"].ToString();
     55            XmlElement xePassword = xmlDoc.CreateElement("Password");
     56            xePassword.InnerText = row["Password"].ToString();
     57
     58            xeUser.AppendChild(xeUserName);
     59            xeUser.AppendChild(xePassword);
     60
     61            root.AppendChild(xeUser);
     62        }

     63
     64        xmlDoc.Save(Server.MapPath("XMLFile.xml"));
     65
     66    }

     67
     68    //
     69    protected void btnWriteXml_Click(object sender, EventArgs e)
     70    {
     71        XmlDocument xmlDoc = new XmlDocument();
     72        xmlDoc.Load(Server.MapPath("XMLFile.xml"));
     73
     74        XmlNode root = xmlDoc.SelectSingleNode("users");
     75
     76        XmlElement xeUser = xmlDoc.CreateElement("user");
     77        xeUser.SetAttribute("sex""");
     78
     79        XmlElement xeUserName = xmlDoc.CreateElement("UserName");
     80        xeUserName.InnerText = "ZhuGuang";
     81        XmlElement xePassword = xmlDoc.CreateElement("Password");
     82        xePassword.InnerText = "MIMA";
     83
     84        xeUser.AppendChild(xeUserName);
     85        xeUser.AppendChild(xePassword);
     86
     87        root.AppendChild(xeUser);
     88
     89        xmlDoc.Save(Server.MapPath("XMLFile.xml"));
     90    }

     91
     92    //
     93    protected void btnReadXml_Click(object sender, EventArgs e)
     94    {
     95        XmlDocument xmlDoc = new XmlDocument();
     96        xmlDoc.Load(Server.MapPath("XMLFile.xml"));
     97
     98        XmlNode root = xmlDoc.SelectSingleNode("users");
     99
    100        XmlNodeList xnlRoot = root.ChildNodes;
    101
    102        foreach (XmlNode xn in xnlRoot)
    103        {
    104            XmlElement xe = (XmlElement)xn;
    105            Response.Write(xe.GetAttribute("sex"+ "<br/>");
    106
    107            XmlNodeList xnlUser = xn.ChildNodes;
    108
    109            foreach(XmlNode xn1 in xnlUser)
    110            {
    111                Response.Write(xn1.InnerText + "<br/>");
    112            }

    113        }

    114    }

    115
    116    //
    117    protected void btnDeleteXmlNode_Click(object sender, EventArgs e)
    118    {
    119        XmlDocument xmlDoc = new XmlDocument();
    120        xmlDoc.Load(Server.MapPath("XMLFile.xml"));
    121
    122        XmlNodeList xnl = xmlDoc.SelectSingleNode("users").ChildNodes;
    123
    124        foreach (XmlNode xn in xnl)
    125        {
    126            XmlElement xe = (XmlElement)xn;
    127            if (xe.GetAttribute("sex"== "男1")
    128            {
    129                xe.RemoveAttribute("sex");
    130                //xe.RemoveAllAttributes();
    131            }

    132            if (xe.GetAttribute("sex"== "男2")
    133            {
    134                xe.RemoveAll();
    135            }

    136        }

    137        xmlDoc.Save(Server.MapPath("XMLFile.xml"));
    138    }

    139
    140    //
    141    protected void btnUpdateXmlNode_Click(object sender, EventArgs e)
    142    {
    143        XmlDocument xmlDoc = new XmlDocument();
    144        xmlDoc.Load(Server.MapPath("XMLFile.xml"));
    145
    146        XmlNodeList xnl = xmlDoc.SelectSingleNode("users").ChildNodes;
    147
    148        foreach (XmlNode xn in xnl)
    149        {
    150            XmlElement xe = (XmlElement)xn;
    151            if (xe.GetAttribute("sex"== "男1")
    152            {
    153                xe.SetAttribute("sex""男男");
    154            }

    155            if (xe.GetAttribute("sex"== "男2")
    156            {
    157                XmlNodeList xnl1 = xe.ChildNodes;
    158                foreach (XmlNode xn1 in xnl1)
    159                {
    160                    XmlElement xe1 = (XmlElement)xn1;
    161                    if (xe1.Name == "UserName")
    162                    {
    163                        xe1.InnerText = "更新成功";
    164                    }

    165                }

    166            }

    167        }

    168        xmlDoc.Save(Server.MapPath("XMLFile.xml"));
    169    }

    170
    171
    172}

    173

    在页面上response XML字符串的话:Response.Write(Server.HtmlEncode(xmlstr))

    代码中注释的地方有,Dataset为我们提供的读取和写入xml文件的方法。

  • 相关阅读:
    select、poll和epoll
    Linux 常用命令之文件和目录
    SmartPlant Review 帮助文档机翻做培训手册
    SmartPlant Foundation 基础教程 3.4 菜单栏
    SmartPlant Foundation 基础教程 3.3 标题栏
    SmartPlant Foundation 基础教程 3.2 界面布局
    SmartPlant Foundation 基础教程 3.1 DTC登陆界面
    SmartPlant Foundation 基础教程 1.4 SPF架构
    SmartPlant Foundation 基础教程 1.3 SPF其他功能
    SmartPlant Foundation 基础教程 1.2 SPF集成设计功能
  • 原文地址:https://www.cnblogs.com/jazzka702/p/1503159.html
Copyright © 2011-2022 走看看