欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    2022年在VisualC#中使用XML指南之读取XML推荐 .pdf

    • 资源ID:27198006       资源大小:212.15KB        全文页数:29页
    • 资源格式: PDF        下载积分:4.3金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要4.3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    2022年在VisualC#中使用XML指南之读取XML推荐 .pdf

    欢迎访问中文C# 技术站 ! 返回首页Free Blog with domains!Domains as low as $1.19/ea, free website and moreGlobe7 - Free Download!Phone + Chat + Radio + TV Free! Download Now!Globe7 - Free !Call Free, Chat Free and Watch TV for Free!返回本类 在 Visual C# 中使用 XML指南之读取 XML 在 Visual C# 中使用 XML指南之读取 XML 2005-02-26 中文 C#技术站对于 XML ,想必各位都比较了解,我也就不用费笔墨来描述它是什么了,我想在未来的 Web 开发中 XML 一定会大放异彩,XML 是可扩展标记语言,使用它企业可以制定一套自己的数据格式, 数据按照这种格式在网络中传输然后再通过XSLT 将数据转换成用户期望的样子表示出来,这样便轻易的解决了数据格式不兼容的问题。用于Internet 的数据传输,我想,这是XML 对于我们这些程序员最诱人的地方!我们今天的主题不是论述XML 的好处,而是讨论在C#中如何使用XML 。下面我们来了解一下使用程序访问XML 的一些基础理论知识。访问的两种模型:在程序中访问进而操作XML 文件一般有两种模型,分别是使用DOM(文档对象模型)和流模型,使用DOM 的好处在于它允许编辑和更新XML 文档,可以随机访问文档中的数据,可以使用XPath 查询,但是, DOM 的缺点在于它需要一次性的加载整个文档到内存中,对于大型的文档,这会造成资源问题。流模型很好的解决了这个问题,因为它对XML 文件的访问采用的是流的概念,也就是说, 任何时候在内存中只有当前节点,但它也有它的不足,它是只读的, 仅向前的, 不能在文档中执行向后导航操作。虽然是各有千秋,但我们也可以在程序中两者并用实现优劣互补嘛,呵呵, 这是题外话了! 我们今天主要讨论XML 的读取,那我们就详细讨论一下流模型吧!流模型中的变体:流模型每次迭代XML 文档中的一个节点,适合于处理较大的文档,所耗内存空间小。流模型中有两种变体“ 推 ” 模型和 “ 拉” 模型。推模型也就是常说的SAX, SAX 是一种靠事件驱动的模型,也就是说:它每发现一个节点就用推模型引发一个事件,而我们必须编写这些事件的处理程序,这样的做法非常的不灵活,也很麻烦。.NET 中使用的是基于“ 拉” 模型的实现方案,“ 拉” 模型在遍历文档时会把感兴趣的文档名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 29 页 - - - - - - - - - 部分从读取器中拉出,不需要引发事件,允许我们以编程的方式访问文档,这大大的提高了灵活性,在性能上“ 拉” 模型可以选择性的处理节点,而SAX 每发现一个节点都会通知客户机,从而,使用“ 拉 ” 模型可以提高Application的整体效率。在.NET 中 “ 拉” 模型是作为XmlReader 类实现的,下面看一下该类的继承结构:我们今天来讲一下该体系结构中的XmlTextReader 类,该类提供对Xml 文件进行读取的功能,它可以验证文档是否格式良好,如果不是格式良好的Xml 文档,该类在读取过程中将会抛出XmlException异常,可使用该类提供的一些方法对文档节点进行读取,筛选等操作以及得到节点的名称和值,请牢记:XmlTextReader 是基于流模型的实现,打个不恰当的比喻, XML 文件就好象水源,闸一开水就流出,流过了就流过了不会也不可以往回流。内存中任何时候只有当前节点,你可以使用 XmlTextReader 类的 Read()方法读取下一个节点。好了,说了这么多来看一个例子,编程要注重实际对吧。看代码前先看下运行效果吧!Example1 按纽遍历文档读取数据,Example2,Example3 按纽得到节点类型,Example4过滤文档只获得数据内容,Example5 得到属性节点, Example6 按纽得到命名空间, Example7显示整个 XML 文档,为此,我专门写一个类来封装以上功能,该类代码如下:/- /XmlReader 类用于 Xml 文件的一般读取操作,以下对这个类做简单介绍:/ /Attributes( 属性 ): /listBox: 设置该属性主要为了得到客户端控件以便于显示所读到的文件的内容(这里是 ListBox 控件 ) /xmlPath: 设置该属性为了得到一个确定的Xml 文件的绝对路径/ /Basilic Using( 重要的引用 ): /System.Xml: 该命名空间中封装有对Xml 进行操作的常用类,本类中使用了其中的 XmlTextReader 类/XmlTextReader: 该类提供对Xml 文件进行读取的功能,它可以验证文档是否格式良 好 , 如 果 不 是 格 式/ 良 好 的Xml文 档 , 该 类 在 读 取 过 程 中 将 会 抛 出XmlException 异常,可使用该类提供的/ 一些方法对文档节点进行读取,筛选等操作以及得到节点的名称和值/ /bool XmlTextReader.Read(): 读取流中下一个节点,当读完最后一个节点再次调用该方法该方法返回false /XmlNodeType XmlTextReader.NodeType: 该属性返回当前节点的类型/ XmlNodeType.Element 元素节点/ XmlNodeType.EndElement 结尾元素节点/ XmlNodeType.XmlDeclaration 文档的第一个节点名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 29 页 - - - - - - - - - / XmlNodeType.Text 文本节点/bool XmlTextReader.HasAttributes: 当前节点有没有属性,返回true 或 false /string XmlTextReader.Name: 返回当前节点的名称/string XmlTextReader.V alue: 返回当前节点的值/string XmlTextReader.LocalName: 返回当前节点的本地名称/string XmlTextReader.NamespaceURI: 返回当前节点的命名空间URI /string XmlTextReader.Prefix: 返回当前节点的前缀/bool XmlTextReader.MoveToNextAttribute(): 移动到当前节点的下一个属性/- namespace XMLReading using System; using System.Xml; using System.Windows.Forms; using System.ComponentModel; / summary / Xml 文件读取器/ /summarypublic class XmlReader : IDisposable private string _xmlPath; private const string _errMsg = Error Occurred While Reading ;private ListBox _listBox; private XmlTextReader xmlTxtRd; #region XmlReader 的构造器public XmlReader() this._xmlPath = string.Empty; this._listBox = null; this.xmlTxtRd = null; / summary/ 构造器/ /summary/ param name=_xmlPathxml 文件绝对路径/param/ param name=_listBox 列表框用于显示xml/parampublic XmlReader(string _xmlPath, ListBox _listBox) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 29 页 - - - - - - - - - this._xmlPath = _xmlPath; this._listBox = _listBox; this.xmlTxtRd = null; #endregion #region XmlReader 的资源释放方法/ summary/ 清理该对象所有正在使用的资源/ /summarypublic void Dispose() this.Dispose(true); GC.SuppressFinalize(this); / summary/ 释放该对象的实例变量/ /summary/ param name=disposing /paramprotected virtual void Dispose(bool disposing) if (!disposing) return; if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); this.xmlTxtRd = null; if (this._xmlPath != null) this._xmlPath = null; #endregion#region XmlReader 的属性名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 29 页 - - - - - - - - - / summary/ 获取或设置列表框用于显示xml / /summarypublic ListBox listBox get return this._listBox; set this._listBox = value; / summary/ 获取或设置xml 文件的绝对路径/ /summarypublic string xmlPath get return this._xmlPath; set this._xmlPath = value; #endregion / summary/ 遍历 Xml 文件/ /summarypublic void EachXml() this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); try 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 29 页 - - - - - - - - - while(xmlTxtRd.Read() this._listBox.Items.Add(this.xmlTxtRd.Value); catch(XmlException exp) throw new XmlException(_errMsg + this._xmlPath + exp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); / summary/ 读取 Xml 文件的节点类型/ /summarypublic void ReadXmlByNodeType() this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); try while(xmlTxtRd.Read() this._listBox.Items.Add(this.xmlTxtRd.NodeType.ToString(); catch(XmlException exp) throw new XmlException(_errMsg + this._xmlPath + exp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 29 页 - - - - - - - - - / summary/ 根据节点类型过滤Xml 文档/ /summary/ param name=xmlNType XmlNodeType 节点类型的数组/parampublic void FilterByNodeType(XmlNodeType xmlNType) this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); try while(xmlTxtRd.Read() for (int i = 0; i xmlNType.Length; i+) if (xmlTxtRd.NodeType = xmlNTypei) this._listBox.Items.Add(xmlTxtRd.Name + is Type + xmlTxtRd.NodeType.ToString(); catch(XmlException exp) throw new XmlException(_errMsg + this.xmlPath + exp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); / summary/ 读取 Xml 文件的所有文本节点值/ /summarypublic void ReadXmlTextV alue() this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 29 页 - - - - - - - - - try while(xmlTxtRd.Read() if (xmlTxtRd.NodeType = XmlNodeType.Text) this._listBox.Items.Add(xmlTxtRd.Value); catch(XmlException xmlExp) throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); / summary/ 读取 Xml 文件的属性/ /summarypublic void ReadXmlAttributes() this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); try while(xmlTxtRd.Read() if (xmlTxtRd.NodeType = XmlNodeType.Element) if (xmlTxtRd.HasAttributes) this._listBox.Items.Add(The Element + xmlTxtRd.Name + has + xmlTxtRd.AttributeCount + Attributes); this._listBox.Items.Add(The Attributes are:); while(xmlTxtRd.MoveToNextAttribute() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 29 页 - - - - - - - - - this._listBox.Items.Add(xmlTxtRd.Name + = + xmlTxtRd.V alue); else this._listBox.Items.Add(The Element + xmlTxtRd.Name + has no Attribute); this._listBox.Items.Add(); catch(XmlException xmlExp) throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); / summary/ 读取 Xml 文件的命名空间/ /summarypublic void ReadXmlNamespace() this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); try while(xmlTxtRd.Read() if (xmlTxtRd.NodeType = XmlNodeType.Element & xmlTxtRd.Prefix != ) this._listBox.Items.Add(The Prefix + xmlTxtRd.Prefix + is associated with namespace + xmlTxtRd.NamespaceURI); this._listBox.Items.Add(The Element with the local name + 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 29 页 - - - - - - - - - xmlTxtRd.LocalName + is associated with + the namespace + xmlTxtRd.NamespaceURI); if (xmlTxtRd.NodeType = XmlNodeType.Element & xmlTxtRd.HasAttributes) while(xmlTxtRd.MoveToNextAttribute() if (xmlTxtRd.Prefix != ) this._listBox.Items.Add(The Prefix + xmlTxtRd.Prefix + is associated with namespace + xmlTxtRd.NamespaceURI); this._listBox.Items.Add(The Attribute with the local name + xmlTxtRd.LocalName + is associated with the namespace + xmlTxtRd.NamespaceURI); catch(XmlException xmlExp) throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); / summary/ 读取整个 Xml 文件/ /summarypublic void ReadXml() string attAndEle = string.Empty; this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 29 页 - - - - - - - - - try while(xmlTxtRd.Read() if (xmlTxtRd.NodeType = XmlNodeType.XmlDeclaration) this._listBox.Items.Add(string.Format(?0 1 ? ,xmlTxtRd.Name,xmlTxtRd.Value); else if (xmlTxtRd.NodeType = XmlNodeType.Element) attAndEle = string.Format( 0 ,xmlTxtRd.Name); if (xmlTxtRd.HasAttributes) while(xmlTxtRd.MoveToNextAttribute() attAndEle = attAndEle + string.Format(0=1 ,xmlTxtRd.Name,xmlTxtRd.Value); attAndEle = attAndEle.Trim() + ; this._listBox.Items.Add(attAndEle); else if (xmlTxtRd.NodeType = XmlNodeType.EndElement) this._listBox.Items.Add(string.Format(/0 ,xmlTxtRd.Name); else if (xmlTxtRd.NodeType = XmlNodeType.Text) this._listBox.Items.Add(xmlTxtRd.Value); catch(XmlException xmlExp) throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString(); finally if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); 窗体代码如下:namespace XMLReading 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 29 页 - - - - - - - - - using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Xml; public class Form1 : System.Windows.Forms.Form private System.Windows.Forms.ListBox listBox1;private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private string xmlPath; private XmlReader xRead; / summary 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 29 页 - - - - - - - - - / 必需的设计器变量。/ /summaryprivate System.ComponentModel.Container components = null;public Form1() InitializeComponent(); / summary / 清理所有正在使用的资源。/ /summaryprotected override void Dispose( bool disposing ) if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 29 页 - - - - - - - - - #region Windows 窗体设计器生成的代码/ summary / 设计器支持所需的方法- 不要使用代码编辑器修改/ 此方法的内容。/ /summaryprivate void InitializeComponent() this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.SuspendLayout(); / / listBox1 / this.listBox1.Anchor = 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 29 页 - - - - - - - - - (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = listBox1; this.listBox1.Size = new System.Drawing.Size(716, 460); this.listBox1.TabIndex = 0; / / button1 / this.button1.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.button1.Location = new System.Drawing.Point(8, 488); this.button1.Name = button1; this.button1.TabIndex = 1; this.button1.Text = Example1; this.button1.Click += new System.EventHandler(this.button1_Click); / / button2 / this.button2.Anchor = 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 29 页 - - - - - - - - - (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.button2.Location = new System.Drawing.Point(96, 488); this.button2.Name = button2; this.button2.TabIndex = 2; this.button2.Text = Example2; this.button2.Click += new System.EventHandler(this.button2_Click); / / button3 / this.button3.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); this.button3.Location = new System.Drawing.Point(648, 488); this.button3.Name = button3; this.button3.TabIndex = 3; this.button3.Text = Example7; this.button3.Click += new System.EventHandler(this.button3_Click); / / button4 / this.button4.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.button4.Location = new System.Drawing.Point(184, 488); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 29 页 - - - - - - - - - this.button4.Name = button4; this.button4.TabIndex = 4; this.button4.Text = Example3; this.button4.Click += new System.EventHandler(this.button4_Click); / / button5 / this.button5.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.button5.Location = new System.Drawing.Point(272, 488); this.button5.Name = button5; this.button5.TabIndex = 5; this.button5.Text = Example4; this.button5.Click += new System.EventHandler(this.button5_Click); / / button6 / this.button6.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.button6.Location = new System.Drawing.Point(360, 488); this.button6.Name = button6; this.button6.TabIndex = 6; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 29 页 - - - - - - - - - this.button6.Text = Example5; this.button6.Click += new System.EventHandler(this.button6_Click); / / button7 / this.button7.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left); this.button7.Location = new System.Drawing.Point(448, 488); this.button7.Name = button7; this.button7.TabIndex = 7; this.button7.Text = Example6; this.button7.Click += new System.EventHandler(this.button7_Click); / / Form1 / this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(728, 517); this.Controls.Add(this.button7); this.Controls.Add(this.button6); this.Controls.Add(t

    注意事项

    本文(2022年在VisualC#中使用XML指南之读取XML推荐 .pdf)为本站会员(Che****ry)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开