C#读取XML文件经典案例

读取XMLListBox/ComboBox


1,知识需求:

(1)访问XML文件的两个基本模型:

一, DOM模型;使用DOM的好处在于它允许编辑和更新XML文档,可以随机访问文档中的数据,可以使用XPath查询,但是,DOM的缺点在于它需要一次性的加载整个文档到内存中,对于大型的文档,这会造成资源问题。

二, 流模型;流模型很好的解决了这个问题,因为它对XML文件的访问采用的是流的概念,也就是说,任何时候在内存中只有当前节点,但它也有它的不足,它是只读的,仅向前的,不能在文档中执行向后导航操作。虽然是各有千秋,但我们也可以在程序中两者并用实现优劣互补C#采用流模型。

流模型每次迭代XML文档中的一个节点,适合于处理较大的文档,所耗内存空间小。流模型中有两种变体——push模型和pull模型
  推模型也就是常说的SAXSAX是一种靠事件驱动的模型,也就是说:它每发现一个节点就用推模型引发一个事件,而我们必须编写这些事件的处理程序,这样的做法非常的不灵活,也很麻烦。
  .NET中使用的是基于模型的实现方案模型在遍历文档时会把感兴趣的文档部分从读取器中拉出,不需要引发事件,允许我们以编程的方式访问文档,这大大的提高了灵活性,在性能上模型可以选择性的处理节点,而SAX每发现一个节点都会通知客户机,从而,使用模型可以提高Application的整体效率。在.NET模型是作为XmlReader(抽象类)实现的

2XmlReader

Represents reader that provides fast, non-cached, forward-only access to XML data.

该类中有三个重要的衍生类:XmlTextReaderXmlTextValidatingReaderXmlNodeReader

3XmlNodeType枚举

该枚举里面有很多实用的数。

2,案例(VS2008+XML

C#读取XML文件经典案例

Form1.cs*

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

 

namespace DemoXmlReader

{

    public partial class XmlForm Form

    {

        public XmlForm()

        {

            InitializeComponent();

        }

 

        private void btnReaderXML_Click(object sender, EventArgs e)

        {

            XmlReader xr new XmlReader(txtPath.Text,lbxml);

            try

            {

              //  xr.EachXmlToListBox();

                xr.ReadXmlTextToListBox();

                //xr.ReadXml();

            }

            catch (XmlException xe)

            

                MessageBox.Show(xe.ToString(), "Error"MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            catch (Exception exp)

            {

                MessageBox.Show(exp.ToString(), "Error"MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

 

            finally

            {

                xr.Dispose();

            }

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog ofd new OpenFileDialog();

            ofd.InitialDirectory Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);   //指定默认打开的窗口指向的文件

            ofd.ShowDialog();

            txtPath.Text ofd.FileName;   //把路径复制给txtPath文本框

        }

 

        private void btnReaderXmlToCb_Click(object sender, EventArgs e)

        {

            XmlReader xr new XmlReader(txtPath.Text, cbxml);

            try

            {

                xr.EachXmlToComboBox();

            }

            catch (XmlException xe)

            {

                MessageBox.Show(xe.ToString(), "Error"MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            catch (Exception exp)

            {

                MessageBox.Show(exp.ToString(), "Error"MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

 

            finally

            {

                xr.Dispose();

            }

 

        }

 

    }

    class XmlReader:IDisposable

    {

        private string xmlPath;

        private const string ERRMSG "Error ocurred While Reading";

        private ListBox listbox;

        private ComboBox combobox;

        private XmlTextReader xtr;

 

        #region XmlReader构造器

        public XmlReader()

        {

            this.xmlPath string.Empty;

            this.xtr null;

            this.listbox null;

        }

        public XmlReader(string xmlPath,ListBox listbox)

        {

            this.xmlPath xmlPath;

            this.listbox listbox;

            

        }

 

        public XmlReader(string xmlPath, ComboBox combobox)

        {

            this.combobox combobox;

            this.xmlPath xmlPath;

        }

        #endregion

 

        #region XmlReader 资源释放方法

        public void Dispose()

        {

            this.Dispose(true);

            GC.SuppressFinalize(this);

        }

        protected virtual void Dispose(bool disposing)

        {

            if (!disposing)

            {

                return;

            }

            if (this.xtr != null)

            {

                

                this.xtr.Close();

                this.xtr null;

            }

            if (this.xmlPath != null)

            {

                this.xmlPath null;

 

            }

        }

        #endregion

 

        #region XmlReader 的属性

        public ListBox listBox

        {

            get return listbox; }

            setlistbox =value;}

        }

        public ComboBox comboBox

        {

            get return combobox; }

            set combobox value}

        }

        public string XmlPath

        {

            get return xmlPath; }

            set xmlPath value}

        }

        #endregion

 

        #region 遍历xml文件

        public void EachXmlToListBox()   

        {

          listbox.Items.Clear();

          xtr new XmlTextReader(xmlPath);

          try

          {

 

              while (xtr.Read())

              {

                  if(xtr.NodeType!=XmlNodeType.XmlDeclaration)

                  listbox.Items.Add(xtr.Value);

                 

              }

 

          }

          catch (XmlException xe)

          {

              throw new XmlException(ERRMSG xe.Message);

          }

          finally {

 

              if (xtr != null)

              {

                  xtr.Close();

              }

          }

 

        }

        public void EachXmlToComboBox()

        {

            combobox.Items.Clear();

            xtr new XmlTextReader(xmlPath);

            try

            {

 

                while (xtr.Read())

                {

                    if (xtr.NodeType != XmlNodeType.XmlDeclaration)

                        combobox.Items.Add(xtr.Value);

 

                }

 

            }

            catch (XmlException xe)

            {

                throw new XmlException(ERRMSG xe.Message);

            }

            finally

            {

 

                if (xtr != null)

                {

                    xtr.Close();

                }

            }

 

        }

 

 

        #endregion

 

        #region 读取XML文件

        public void ReadXml()

        {

            string attAndEle string.Empty;

            listbox.Items.Clear();

            this.xtr new XmlTextReader(this.xmlPath);  //通过路径,读取XML文件

            try{

                while (xtr.Read())  //按照流方式一个节点一个节点的被动读取。 if the next node was read successfully,return true

                {

                    //public enum XmlNodeType 为枚举类型,None Element Attribute Text CDATA EntityReference Entity ProcessingInstruction Comment Document DocumentType DocumentFragment Notation Whitespace SignificantWhitespace EndElement EndEntity XmlDeclaration 

                    if (xtr.NodeType == XmlNodeType.XmlDeclaration)  //XmlDeclaration为XML 声明(例如,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值