.NET XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;


public partial class CreateXml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    /// <summary>
    /// 创建Xml
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        XmlDataDocument XmlDoc = new XmlDataDocument();
        //创建Xml文档的说明信息
        XmlDeclaration XmlDec= XmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);// 版本 编码  独立?
        XmlDoc.AppendChild(XmlDec);

        //创建根节点
        XmlElement XmlEle = XmlDoc.CreateElement("Book");
        XmlDoc.AppendChild(XmlEle);

        //创建新节点
        XmlElement BookOne = XmlDoc.CreateElement("BookOne");
        //创建新节点的子节点
        XmlElement BookName = XmlDoc.CreateElement("BookName");
        //子节点的值
        BookName.InnerText = "《like one》";
        //创建新节点的子节点
        XmlElement BookPrice = XmlDoc.CreateElement("BookPrice");
        //子节点的值
        BookPrice.InnerText = "10.00$";
        //将子节点附加给新节点
        BookOne.AppendChild(BookName);
        BookOne.AppendChild(BookPrice);
        XmlEle.AppendChild(BookOne);
        //将新节点附加个根节点
        XmlDoc.AppendChild(XmlEle);

        XmlElement BookOne2 = XmlDoc.CreateElement("BookOne");
        XmlElement BookName2 = XmlDoc.CreateElement("BookName");
        BookName2.InnerText = "《like two》";
        XmlElement BookPrice2 = XmlDoc.CreateElement("BookPrice");
        BookPrice2.InnerText = "10.00$";
        BookOne2.AppendChild(BookName2);
        BookOne2.AppendChild(BookPrice2);
        XmlEle.AppendChild(BookOne2);
        XmlDoc.AppendChild(XmlEle);

        XmlElement BookOne3 = XmlDoc.CreateElement("BookOne");
        //给BookOne3 设置属性
        BookOne3.SetAttribute("Attribute1","111");
        BookOne3.SetAttribute("Attribute2", "222");

        XmlElement BookName3 = XmlDoc.CreateElement("BookName"); 
        BookName3.InnerText = "《like three》";
        XmlElement BookPrice3 = XmlDoc.CreateElement("BookPrice");
        BookPrice3.InnerText = "10.00$";
        BookOne3.AppendChild(BookName3);
        BookOne3.AppendChild(BookPrice3);
        XmlEle.AppendChild(BookOne3);
        XmlDoc.AppendChild(XmlEle);

        XmlDoc.Save(Server.MapPath("~/")+"Test.xml");
    }

    /// <summary>
    /// 获取XML文档节点的值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        XmlDocument Xmldoc = new XmlDocument();
        Xmldoc.Load(Server.MapPath("~/Test.xml"));

        //首节点的值
        XmlNode  Node= Xmldoc.DocumentElement.FirstChild;
        
        if (Node != null)
        {
            string str = "";
            str += "This is " + Node.Name + " and childname is " + Node["BookName"].Name+Node["BookName"].InnerText + "<br/>";
            str += Node["BookPrice"].Name + " is " + Node["BookPrice"].InnerText + "<br/><br/><br/><br/>";
            Response.Write(str);
        }

        //末节点的值
        Node = Xmldoc.DocumentElement.LastChild;
        if (Node != null)
        {
            string str = "";
            str += "This is " + Node.Name + " and childname is " + Node["BookName"].Name + Node["BookName"].InnerText + "<br/>";// Node["BookName"].Name 就是BookName    Node["BookName"]即节点名字为BookName
            str += Node["BookPrice"].Name + " is " + Node["BookPrice"].InnerText + "<br/><br/><br/><br/>";
            Response.Write(str);
        }
        
    }


    /// <summary>
    /// 查看某一节点的值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSome_Click(object sender, EventArgs e)
    {
        XmlDocument Xmldoc = new XmlDocument();
        Xmldoc.Load(Server.MapPath("~/Test.xml"));
        //获取XML文档的节点列表
        XmlNodeList NodeList=Xmldoc.DocumentElement.ChildNodes;
        //节点列表的首个节点 索引以0开始
        XmlNode Node = NodeList.Item(0);
        if (Node != null)
        {
            string str = "";
            str += "This is " + Node.Name + " and childname is " + Node["BookName"].Name + Node["BookName"].InnerText + "<br/>";
            str += Node["BookPrice"].Name + " is " + Node["BookPrice"].InnerText + "<br/><br/><br/><br/>";
            Response.Write(str);
        }
    }


    /// <summary>
    /// Xml节点定位插入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(Server.MapPath("~/Test.xml"));
        //寻找根节点
        XmlNode xmlnode = XmlDoc.SelectSingleNode("/Book");

        XmlElement BookOne = XmlDoc.CreateElement("BookOne");
        XmlElement BookName = XmlDoc.CreateElement("BookName");
        BookName.InnerText = "《like inert》";
        XmlElement BookPrice = XmlDoc.CreateElement("BookPrice");
        BookPrice.InnerText = "10.00$";
        BookOne.AppendChild(BookName);
        BookOne.AppendChild(BookPrice);

        //将新节点附加给根节点  使用XmlDoc.AppendChild 需将新节点先附加给根节点 再将根节点附加给文档 新节点也可以用XmlDoc.DocumentElement.AppendChild 直接附加给文档
        //xmlnode.AppendChild(BookOne);
        //XmlDoc.AppendChild(xmlnode);

        //在末尾添加子节点
        //XmlDoc.DocumentElement.AppendChild(BookOne);

        //在指定位置添加节点 InsertBefore
        //XmlDoc.DocumentElement.InsertBefore(BookOne,XmlDoc.DocumentElement.ChildNodes.Item(0));

        //在指定位置添加节点 InsertBefore
        XmlDoc.DocumentElement.InsertAfter(BookOne, XmlDoc.DocumentElement.ChildNodes.Item(0));
        XmlDoc.Save(Server.MapPath("~/") + "Test.xml");


    }

    /// <summary>
    /// 删除节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(Server.MapPath("~/Test.xml"));


        //寻找根节点
        XmlNode xmlnode = XmlDoc.SelectSingleNode("/Book");
        //删除根节点的第一个子节点
        //XmlNode Node = xmlnode.FirstChild;
        //if (Node != null)
        //{
        //    Node.ParentNode.RemoveChild(Node);
        //}

        //根据条件循环删除节点
        XmlNodeList NodeList = XmlDoc.DocumentElement.ChildNodes;
        for (int i = 0; i < NodeList.Count; i++)
        {
            if (NodeList[i].FirstChild.InnerText == "《like inert》")
            {
                NodeList[i].ParentNode.RemoveChild(NodeList[i]);
            }
        }

        XmlDoc.Save(Server.MapPath("~/") + "Test.xml");

    }


    /// <summary>
    /// 编辑节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(Server.MapPath("~/Test.xml"));


        //寻找根节点
        XmlNode xmlnode = XmlDoc.SelectSingleNode("/Book");
        //编辑根节点的第一个子节点
        XmlNode Node = xmlnode.FirstChild;
        if (Node != null)
        {
            Node.FirstChild.InnerText = "i am edited " + Node.FirstChild.InnerText;
            //指定第几个子节点
            Node.ChildNodes[1].InnerText = "i am edited " + Node.ChildNodes[1].InnerText;

            //Node.FirstChild.InnerText = Node.FirstChild.InnerText.Replace("i am edited ", "");
            //Node.ChildNodes[1].InnerText = Node.ChildNodes[1].InnerText.Replace("i am edited ", "");
        }

        //根据条件循环编辑节点
        XmlNodeList NodeList = XmlDoc.DocumentElement.ChildNodes;
        for (int i = 0; i < NodeList.Count; i++)
        {
            //根据条件修改
            //if (NodeList[i].FirstChild.InnerText == "《like inert》")
            //{
            //    NodeList[i].FirstChild.InnerText = "i am edited " + NodeList[i].FirstChild.InnerText;
            //}

            //NodeList[i].FirstChild.InnerText = "i am edited " + NodeList[i].FirstChild.InnerText;

            //循环修改信息
            NodeList[i].ChildNodes[0].InnerText = "i am edited " + NodeList[i].ChildNodes[0].InnerText;
            NodeList[i].ChildNodes[1].InnerText = "i am edited " + NodeList[i].ChildNodes[1].InnerText;
        }

        XmlDoc.Save(Server.MapPath("~/") + "Test.xml");
    }

 

    /// <summary>
    /// 查找
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
       
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("~/Test.xml"));
        XmlNodeList nodelist = xmlDoc.SelectNodes("Book/BookOne[BookName='" + this.tbSearch.Text.Trim() + "']");
        for (int i = 0; i < nodelist.Count; i++)
        {
            string str = "";
            str += nodelist[i].InnerText;
            Response.Write(str);
        }
        //Response.Write(nodelist.Count);
    }

    /// <summary>
    /// 模糊查找
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("~/Test.xml"));
        //xmlDoc.SelectNodes 搜索多个节点 xmlDoc.SelectSingleNode  搜索根节点
        XmlNodeList nodelist = xmlDoc.SelectNodes("Book/BookOne[contains(BookName,'" + this.tbSearch2.Text.Trim() + "')]");

        //for (int i = 0; i < nodelist.Count; i++)
        //{
        //    string str = "";
        //    str += nodelist[i].InnerText;
        //    Response.Write(str);
        //}
        Response.Write(nodelist.Count);
    }

    
    /// <summary>
    /// 获取节点属性信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAttra_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("~/Test.xml"));

        //文档节点列
        XmlNodeList nodelist = xmlDoc.DocumentElement.ChildNodes;
        for (int i = 0; i < nodelist.Count; i++)
        { 
            //其中一个节点的属性集合
            XmlAttributeCollection xmlattr=nodelist[i].Attributes;

            foreach(XmlAttribute xmla in xmlattr)
            {
                //判断该节点的属性名字和值
                if (xmla.Name == "Attribute1"&&xmla.Value=="111")
                {
                    Response.Write(nodelist[i].ChildNodes[0].InnerText);
                }

                if (xmla.Name == "Attribute2" && xmla.Value == "222")
                {
                    Response.Write(nodelist[i].ChildNodes[1].InnerText);
                }
            }
        }
    }
}


 

 

 

<div>
        <asp:Button ID="btnCreate" runat="server" Text="创建XML" 
            onclick="btnCreate_Click" />
            
        &nbsp;&nbsp;
            
        <asp:Button ID="btnFirst" runat="server" Text="查找Xml首节点值" 
            onclick="btnFirst_Click" />
        <br />
        <asp:Button ID="btnSome" runat="server" Text="查看某一节点的值" 
            onclick="btnSome_Click" />
        <br />
        <br />
        <asp:Button ID="btnInsert" runat="server" Text="Xml节点定位插入" 
            onclick="btnInsert_Click" />
            
        &nbsp;&nbsp;
            
        <asp:Button ID="btnDelete" runat="server" Text="删除某个节点" 
            onclick="btnDelete_Click" />
        <br />
        <br />
        <asp:Button ID="btnEdit" runat="server" Text="编辑节点" onclick="btnEdit_Click" />
        <br />
        <br />
        <asp:Button ID="btnAttra" runat="server" onclick="btnAttra_Click" Text="获取属性" />
        <br />
        <br />
        <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" Text="查找" />
        <br />
        <br />
        <asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnSearchs" runat="server" onclick="Button1_Click" Text="模糊查询" />
        <br />
        <br />
        <asp:TextBox ID="tbSearch2" runat="server"></asp:TextBox>
    </div>

 

Test.XML

<?xml version="1.0" encoding="gb2312"?>
<Book>
  <BookOne>
    <BookName>《like one》</BookName>
    <BookPrice>10.00$</BookPrice>
  </BookOne>
  <BookOne>
    <BookName>《like two》</BookName>
    <BookPrice>10.00$</BookPrice>
  </BookOne>
  <BookOne Attribute1="111" Attribute2="222">
    <BookName>《like three》</BookName>
    <BookPrice>10.00$</BookPrice>
  </BookOne>
</Book>

 

posted @ 2012-04-20 23:31  sidihu  阅读(201)  评论(0编辑  收藏  举报