今天瞎写的关于XML的一些。

2018-06-23 23:45:51来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

using System;
using System.Windows.Forms;
using System.Xml;

namespace winformDemo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //XmlDocument打开文档
        XmlDocument xc = new XmlDocument();
        /// <summary>
        /// 显示所有商品的名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            // xc.Load(写入要获取文档的路径)
            xc.Load("D:\\Text\\shopping.xml");
            //XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;获取shopping下面的子节点放到集合里。
            XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;
            //循环去取子节点
            foreach (XmlNode xn in xnl)
            {
                //获取商品名字。
                //this.textBox1.Text += xn.Attributes["id"].Value + "\r\n";
                foreach (XmlNode xnd in xn.ChildNodes)
                {
                    if (xnd.Name == "Shopname")
                    {
                        textBox1.Text += "商品名字:" + xnd.InnerText + "\r\n";
                    }
                }
            }
        }
        /// <summary>
        /// 显示所有商品信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox2.Text = "";
            // xc.Load(写入要获取文档的路径)
            xc.Load("D:\\Text\\shopping.xml");
            //XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;获取shopping下面的子节点放到集合里。
            XmlNodeList xnl = xc.SelectSingleNode("shopping").ChildNodes;
            //XmlNodeList xnl = xc.SelectSingleNode("/shopping/shop[last()]").ChildNodes;
            //XmlNodeList xnl = xc.SelectSingleNode("/shopping/shop[@id=2]").ChildNodes;
            //循环去取子节点
            foreach (XmlNode xn in xnl)
            {
                //取最后一条数据

                //textBox2.Text += "商品名字:" + xn.InnerText + "\r\n";
                //获取所有商品的信息。
                textBox2.Text += "商品ID:" + xn.Attributes["id"].Value + "\r\n";
                textBox2.Text += "商品名字:" + xn.ChildNodes[0].InnerText + "\r\n";
                textBox2.Text += "商品价格:" + xn.ChildNodes[1].InnerText + "\r\n";
                textBox2.Text += "商品数量:" + xn.ChildNodes[2].InnerText + "\r\n";
            }
        }
        /// <summary>
        /// 读取所有信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox3.Text = "";
            XmlReader reader = XmlReader.Create(@"D:\\Text\\shopping.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Attribute:
                        for (int i = 0; i < reader.AttributeCount; i++)
                        {
                            reader.MoveToAttribute(i);
                            this.textBox3.Text += "属性:" + reader.Name + "=" + reader.Value + "\r\n";
                        }
                        break;
                    case XmlNodeType.Element:
                        this.textBox3.Text += "节点名称:" + reader.LocalName + "\r\n";
                        for (int i = 0; i < reader.AttributeCount; i++)
                        {
                            reader.MoveToAttribute(i);
                            this.textBox3.Text += "属性:" + reader.Name + "=" + reader.Value + "\r\n";
                        }
                        break;
                    case XmlNodeType.Text:
                        this.textBox3.Text += "节点内容:" + reader.Value + "\r\n";
                        break;
                }
            }
        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            XmlWriter writer = XmlWriter.Create(@"D:\\Text\\goods.xml");
            writer.WriteStartElement("dog");
            writer.WriteAttributeString("color", "black");
            writer.WriteString("How can I be sure");
            writer.WriteEndDocument();
            writer.Close();
        }
    }
}

添加XML 是直接添加进去了,没有显示出来。所以不要重复点击,可以去文本里看。

总结的一些:
                        XML
xml:
    (超级标记语言)又称(可扩展标记语言)(简单的数据存储语言)(数据存储技术)
格式:
    gb2312,UTF-8;
数据库的存储是XML格式。
特点:
    跨平台、跨语言、跨系统、跨浏览器、节点名自定义、封闭、区分大小写、属性自定义、唯一根节点。
C#提供XML的对象和方法:
    XMLDocument(xml文档):(load(路径)、SelectSingleNode(获取根节点)、childNodes(子节点))
    XMLNodelist(节点集合):(XmlNodeList list=根节点下节点)
    XMLNode(节点):(属性:text、innerText、name、attributrs[]、value、childNodes)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:关于C#委托和Lambda表达式

下一篇:Sql Server数据库使用触发器和sqlbulkcopy大批量数据插入更新