已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第十二章在 NET中处理XML 2007iSoftStoneHoldingsLtd AllRightsReserved 2 回顾 跟踪是ASP NET引入的新功能 只需对页面和应用程序启用此功能 就可以查看有关单个ASP NET页请求的诊断信息页面级跟踪具有一个称为TraceMode的属性 此属性接受SortByCategory或SortByTime属性值 以决定如何显示输出结果应用程序级跟踪将启动对整个应用程序的跟踪 需要在web config配置文件中进行设置缓存是一项在计算中广泛用来提高性能的技术 它将访问频率高的数据或构造成本高的数据保留在内存中输出缓存将缓存整个页面 数据缓存将设置内存变量缓存使用VS NET的 Web安装项目 可以将应用程序打包成易于部署的形式 2007iSoftStoneHoldingsLtd AllRightsReserved 3 目标 理解XMLDocument结构使用XML命名空间中的XMLDocument对象使用XMLReader和XMLWriter对象使用XMLValidatingReader填充XML文件中的数据集使用数据集创建XML文档 2007iSoftStoneHoldingsLtd AllRightsReserved 4 XML文档结构 标准 基于文本的数据标准 通信 XML 用于定义语义标签的规则集合 将文档分解为多个部分 标识文档各部分 元数据是定义用于定义其他特定于域的语义和结构化标记语言的语法用途 2007iSoftStoneHoldingsLtd AllRightsReserved 5 组织XML数据 标识使用的版本 演示根元素的示例代码 Phonebook元素具有开始标签和结束标签 1998 具有子元素的Phonebook元素 2007iSoftStoneHoldingsLtd AllRightsReserved 6 System Xml命名空间 System Xml命名空间包含了处理Xml的类 以下是经常用到的类XmlTextReader类提供对XML数据的快速 非高速缓存的只进读访问XmlValidatingReader类提供DTD XDR和XSDSchema验证XmlTextWriter类提供一种生成XML的快速只进方法XmlDataDocument类提供XmlDocument的实现 此XmlDocument可以与数据集关联 可以通过数据集的关系表示或XmlDataDocument的树表示 来同时查看和操作结构化的XMLXPathNavigator类提供W3CXPath1 0数据模型 而不是用于导航的光标样式模型的存储XslTransform类是一个W3CXSLT1 0规范兼容的XSLT处理器 用于转换XML文档XmlSchema对象模型类提供一组直接反映W3CXSD规范的可导航类 这些类支持在程序中创建XSDSchema 2007iSoftStoneHoldingsLtd AllRightsReserved 7 XMLDocument对象 内存 XML文档 XML文档对象模型 XMLDOM 类 提供了对读取 写入和操作XML文档的支持 XML文档对象 执行任务 如加载和保存文档 包括Load LoadXML 和Save 用于访问文档中的所有节点 2007iSoftStoneHoldingsLtd AllRightsReserved 8 XML节点类型 2007iSoftStoneHoldingsLtd AllRightsReserved 9 加载XML文档3 1 将XML文档加载到变量中 Load 方法 如果不能解析或访问URL 从指定位置加载XML文档 返回错误 并将文档对象的documentElement属性设置为null 2007iSoftStoneHoldingsLtd AllRightsReserved 10 加载XML文档3 2 加菲尔德5555555纽约26 10 1978迈克6666666纽约12 02 1978 privatevoidPage Load objectsender System EventArgse XmlDocumentMyPhone newXmlDocument MyPhone Load C Inetpub wwwroot aspex CH12 phone xml lblXmlData Text MyPhone InnerXml ToString 2007iSoftStoneHoldingsLtd AllRightsReserved 11 加载XML文档3 3 LoadXML 方法 使用提供的字符串加载XML文档 加载成功则返回true 加载失败则返回false 并将文档对象的documentElement属性设置为null XmlDocumentMyPhone newXmlDocument stringxmlString 加菲尔德 5555555 北京纽约 26 10 1978 迈克 6666666 纽约 12 02 1978 MyPhone LoadXml xmlString 2007iSoftStoneHoldingsLtd AllRightsReserved 12 获取节点和节点属性 XmlDocumentMyPhone newXmlDocument MyPhone Load Server MapPath phone xml 获取根元素XmlElementroot MyPhone DocumentElement XmlElementphone root ChildNodes 0 获取节点 节点属性 2007iSoftStoneHoldingsLtd AllRightsReserved 13 使用Save方法编写XML数据 XmlDocumentMyPhone newXmlDocument MyPhone Load Server MapPath phone xml XmlElementnewFriendsName MyPhone CreateElement Name newFriendsName InnerText 斯坦利 MyPhone DocumentElement InsertAfter newFriendsName MyPhone DocumentElement LastChild XmlElementnewFriendsNumber MyPhone CreateElement Number newFriendsNumber InnerText 7777777 MyPhone DocumentElement InsertAfter newFriendsNumber MyPhone DocumentElement LastChild XmlElementnewFriendsCity MyPhone CreateElement City newFriendsCity InnerText 加利福尼亚 MyPhone DocumentElement InsertAfter newFriendsCity MyPhone DocumentElement LastChild XmlElementnewFriendsDOB MyPhone CreateElement DateOfBirth newFriendsDOB InnerText 12 06 1984 MyPhone DocumentElement InsertAfter newFriendsDOB MyPhone DocumentElement LastChild MyPhone Save Server MapPath phone1 xml Response Write 数据已写入 单击 加载 查看Phone xml 输出结果 2007iSoftStoneHoldingsLtd AllRightsReserved 14 从XML文档中读取节点2 1 从XML文档中读取节点 XmlReader 提供对XML数据流的快速访问 另一个派生类是XmlValidatingReader 读取XML数据 并支持DTD和Schema验证 由用于读取基于文本的XML文件的XmlTextReader类实现 一个抽象类 用于确定各种因素 如节点的深度 2007iSoftStoneHoldingsLtd AllRightsReserved 15 从XML文档中读取节点2 2 usingSystem usingSystem Collections usingSystem ComponentModel usingSystem Data usingSystem Xml usingSystem Drawing usingSystem Web usingSystem Web SessionState usingSystem Web UI usingSystem Web UI WebControls usingSystem Web UI HtmlControls namespaceCH12 publicclassXMLReaderDemo System Web UI Page protectedSystem Web UI WebControls LabellblRecords privatevoidPage Load objectsender System EventArgse stringfileName Server MapPath Phone xml XmlTextReaderobjXmlTextReader newXmlTextReader fileName while objXmlTextReader Read if objXmlTextReader NodeType XmlNodeType Element if objXmlTextReader LocalName Equals Name objXmlTextReader LocalName Equals Number lblRecords Text objXmlTextReader ReadString t 2007iSoftStoneHoldingsLtd AllRightsReserved 16 在XML文档中写入节点2 1 在XML文档中写入节点 XmlWriter XMLTextWriter类派生自该类 一个抽象类 帮助创建XML流并将数据写入到格式良好的XML文档 2007iSoftStoneHoldingsLtd AllRightsReserved 17 在XML文档中写入节点2 2 usingSystem usingSystem Collections usingSystem ComponentModel usingSystem Data usingSystem Xml usingSystem Drawing usingSystem Web usingSystem Web SessionState usingSystem Web UI usingSystem Web UI WebControls usingSystem Web UI HtmlControls namespaceCH12 publicclassXmlWriterDemo System Web UI Page privatevoidPage Load objectsender System EventArgse stringFileName C Inetpub wwwroot aspex CH12 phone xml XmlTextWriterobjXmlTextWriter newXmlTextWriter FileName null objXmlTextWriter Formatting Formatting Indented objXmlTextWriter Indentation 6 objXmlTextWriter WriteStartDocument objXmlTextWriter WriteStartElement PhoneBook objXmlTextWriter WriteStartElement Name objXmlTextWriter WriteString 加菲尔德 objXmlTextWriter WriteEndElement objXmlTextWriter WriteStartElement Number objXmlTextWriter WriteString 5555555 objXmlTextWriter WriteEndElement objXmlTextWriter WriteStartElement City objXmlTextWriter WriteString 纽约 objXmlTextWriter WriteEndElement objXmlTextWriter WriteStartElement DateOfBirth objXmlTextWriter WriteString 26 10 1978 objXmlTextWriter WriteEndElement objXmlTextWriter WriteEndElement objXmlTextWriter WriteEndDocument objXmlTextWriter Flush objXmlTextWriter Close 输出结果 2007iSoftStoneHoldingsLtd AllRightsReserved 18 使用XML验证读取器验证XML数据 2007iSoftStoneHoldingsLtd AllRightsReserved 19 XML和数据集 数据集 存储XML数据 而不会引发任何错误 导入System IO数据以读取XML数据 列的值列的值列的值列的值 数据集读取以下格式的数据 2007iSoftStoneHoldingsLtd AllRightsReserved 20 使用ReadXml方法读取XML数据2 1 FileStreammyfs newFileStream Server MapPath xmldatagrid xml FileMode Open FileAccess Read 如何打开XML文件 打开文件之后 定义一个StreamReader并把FileStream传入 StreamReadermyreader newStreamReader myfs 数据集读取StreamReader ReadXml 方法中的XML数据 DataSetmyds newDataSet myds ReadXml myreader 需要将XML数据绑定到数据集 DataViewmySource newDataView myds Tables 0 dbgMyGrid DataSource mySource dbgMyGrid DataBind 2007iSoftStoneHoldingsLtd AllRightsReserved 21 使用ReadXml方法读取XML数据 Net开发者精彩站点学习XML与ASP NETMicrosoftASP NET官方网站 usingSystem usingSystem Collections usingSystem ComponentModel usingSystem IO usingSystem Data usingSystem Drawing usingSystem Web usingSystem Web SessionState usingSystem Web UI usingSystem Web UI WebControls usingSystem Web UI HtmlControls namespaceCH12 publicclassReadXml System Web UI Page protectedSystem Web UI WebControls LabellblTableName protectedSystem Web UI WebControls DataGriddgMyGrid privatevoidPage Load objectsender System EventArgse DataSetmyds newDataSet FileStreammyfs newFileStream Server MapPath xmldatagrid xml FileMode Open FileAccess Read StreamReadermyreader newStreamReader myfs myds ReadXml myreader myfs Close DataViewmySource newDataView myds Tables 0 blTableName Text lblTableName Text mySource Table TableName dgMyGrid DataSource mySource dgMyGrid DataBind 2007iSoftStoneHoldingsLtd AllRightsReserved 22 使用WriteXML方法写入XML数据 usingSystem usingSystem Collections usingSystem ComponentModel usingSystem Data usingSystem Data SqlClient usingSystem Drawing usingSystem Web usingSystem Web SessionState usingSystem Web UI usingSystem Web UI WebControls usingSystem Web UI HtmlControls namespaceCH12 publicclassWritexml S
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 研究生学业奖学金申请审批表
- 广东2025年下半年东莞市事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 山东质量技术监督局所属事业单位2025年下半年招考工作人员易考易错模拟试题(共500题)试卷后附参考答案
- 出租车包车协议合同
- 山东2025年下半年郓城县招考事业单位人员易考易错模拟试题(共500题)试卷后附参考答案
- 村委租用土地协议书
- 天津市水产局所属事业单位2025年下半年招考专业技术人员易考易错模拟试题(共500题)试卷后附参考答案
- 国航股份综合保障部2025年下半年校园招聘易考易错模拟试题(共500题)试卷后附参考答案
- 框架签约协议书模板
- 桑拿房设计合同范本
- 战略性矿产资源的探产供储销体系优化
- 中药热奄包课件
- 2025年租车合同范本下载(模板)
- 学生实习安全及突发事件应急预案
- 《红日》读书分享模板
- 机械伤害事故应急演练方案(现场处置方案)
- 技术经理人考试题库及答案
- 钢结构桁架吊装安装专项施工方案
- 12.3.2 等腰三角形的判定
- 无人机飞行控制技术 课件 第5-8章 固定翼无人机典型飞行控制系统分析- 无人机测控系统
- 全国消防设施操作员中级理论真题(含答案)
评论
0/150
提交评论