今天瞎写的关于XML的一些。_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 今天瞎写的关于XML的一些。

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

 2017/8/21 21:09:13  忧叹ic  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Windows.Forms;usingSystem.Xml;namespacewinformDemo{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}//XmlDocument打开文档XmlDocumentxc=newXmlDocument();///<summary>///显示所有商品的名字///</summary>///<
  • 标签:XML

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)

发表评论
用户名: 匿名