今天瞎写的关于XML的一些。 数据库 第1张

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)