Life is Good.

Enhance Tech and English
随笔 - 65, 文章 - 20, 评论 - 21, 引用 - 0
数据加载中……

.NET中XML操作(C#)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;

public partial class _2008_Default2 : System.Web.UI.Page
{
    XmlDocument xmldoc;
    XmlNode xmlnode;
    XmlElement xmlelem;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void makeXML()
    {
        xmldoc = new XmlDocument();
        //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
        XmlDeclaration xmldecl;
        xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
        xmldoc.AppendChild(xmldecl);

        //加入一个根元素
        xmlelem = xmldoc.CreateElement("", "Employees", "");
        xmldoc.AppendChild(xmlelem);
        //加入另外一个元素
        for (int i = 0; i < 2; i++)
        {

            XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees>
            XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点
            xe1.SetAttribute("genre", "李赞红");//设置该节点genre属性
            xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性

            XmlElement xesub1 = xmldoc.CreateElement("title");
            xesub1.InnerText = "CS从入门到精通";//设置文本节点
            xe1.AppendChild(xesub1);//添加到<Node>节点中
            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);//添加到<Employees>节点中
        }
        //保存创建好的XML文档
        xmldoc.Save(Server.MapPath("data.xml"));
    }

    private void makeXMl2()
    {
        XmlTextWriter xmlWriter;
        string strFilename = Server.MapPath("data1.xml");

        xmlWriter = new XmlTextWriter(strFilename, Encoding.Default);//创建一个xml文档
        xmlWriter.Formatting = Formatting.Indented;
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("Employees");

        xmlWriter.WriteStartElement("Node");
        xmlWriter.WriteAttributeString("genre", "李赞红");
        xmlWriter.WriteAttributeString("ISBN", "2-3631-4");

        xmlWriter.WriteStartElement("title");
        xmlWriter.WriteString("CS从入门到精通");
        xmlWriter.WriteEndElement();

        xmlWriter.WriteStartElement("author");
        xmlWriter.WriteString("候捷");
        xmlWriter.WriteEndElement();

        xmlWriter.WriteStartElement("price");
        xmlWriter.WriteString("58.3");
        xmlWriter.WriteEndElement();

        xmlWriter.WriteEndElement();

        xmlWriter.Close();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        makeXML();
        makeXMl2();
    }

    private void addxmldata()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));
        XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找<Employees>
        XmlElement xe1 = xmlDoc.CreateElement("Node");//创建一个<Node>节点
        xe1.SetAttribute("genre", "张三");//设置该节点genre属性
        xe1.SetAttribute("ISBN", "1-1111-1");//设置该节点ISBN属性

        XmlElement xesub1 = xmlDoc.CreateElement("title");
        xesub1.InnerText = "C#入门帮助";//设置文本节点
        xe1.AppendChild(xesub1);//添加到<Node>节点中
        XmlElement xesub2 = xmlDoc.CreateElement("author");
        xesub2.InnerText = "高手";
        xe1.AppendChild(xesub2);
        XmlElement xesub3 = xmlDoc.CreateElement("price");
        xesub3.InnerText = "158.3";
        xe1.AppendChild(xesub3);

        root.AppendChild(xe1);//添加到<Employees>节点中
        xmlDoc.Save(Server.MapPath("data.xml"));


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        addxmldata();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        deleteXML();
    }
    private void deleteXML()//删除结点
    {
        XmlDocument xmlDoc=new XmlDocument();
        xmlDoc.Load( Server.MapPath("data.xml") );
        XmlNode root=xmlDoc.SelectSingleNode("Employees");
        XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes;
        for (int i = 0; i < xnl.Count; i++)
        {
            XmlElement xe = (XmlElement)xnl.Item(i);
            if (xe.GetAttribute("genre") == "张三")
            {
                root.RemoveChild(xe);
                if (i < xnl.Count) i = i - 1;
            }
        }
        xmlDoc.Save(Server.MapPath("data.xml"));
    }

    private void updateXML()//修改结点的值(属性和子结点)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));

        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点

        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 = "亚胜";//则修改
                    }
                }
            }
        }
        xmlDoc.Save(Server.MapPath("data.xml"));//保存。

    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        updateXML();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        updateXML2();
    }

    private void updateXML2()//修改结点(添加结点的属性和添加结点的自结点
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));

        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点

        foreach (XmlNode xn in nodeList)
        {
            XmlElement xe = (XmlElement)xn;
            xe.SetAttribute("test", "111111");

            XmlElement xesub = xmlDoc.CreateElement("flag");
            xesub.InnerText = "1";
            xe.AppendChild(xesub);
        }
        xmlDoc.Save(Server.MapPath("data.xml"));
    }


    protected void Button6_Click(object sender, EventArgs e)
    {
        RemoveChild();
    }

    private void RemoveChild()//删除结点中的某一个属性
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("data.xml"));
        XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes;
        foreach (XmlNode xn in xnl)
        {
            XmlElement xe = (XmlElement)xn;
            xe.RemoveAttribute("genre");//删除genre属性

            XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
            foreach (XmlNode xn1 in nls)//遍历
            {
                XmlElement xe2 = (XmlElement)xn1;//转换类型
                if (xe2.Name == "flag")//如果找到
                {
                    xe.RemoveChild(xe2);//则删除
                }
            }
        }
        xmlDoc.Save(Server.MapPath("data.xml"));
    }

    private void readXMl()//按照文本文件读取xml
    {
        System.IO.StreamReader myFile = new System.IO.StreamReader(Server.MapPath("data.xml"), System.Text.Encoding.Default);
        //注意System.Text.Encoding.Default

        string myString = myFile.ReadToEnd();//myString是读出的字符串
        myFile.Close();

        Response.Write(myString);

    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        readXMl();
    }
}

posted on 2010-05-31 16:17 Mike Song 阅读(235) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理