版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
上機題1、<?xmlversion="1.0"encoding="gb2312"?><book><title>XML指南</title><chapter>XML入門簡介<para>什么是HTML</para><para>什么是XML</para></chapter><chapter>XML語法<para>XML元素必須有結(jié)束標(biāo)簽</para><para>XML元素必須正確的嵌套</para></chapter></book>2、<?xmlversion="1.0"encoding="gb2312"?><employees><employee><id>1</id><name>張三</name><age>32</age><sex>男</sex><address>上海</address></employee><employee><id>2</id><name>李四</name><age>22</age><sex>男</sex><address>北京</address></employee></employees>3、<?xmlversion="1.0"encoding="gb2312"?><students><student><number>1</number><name>tom</name><score>100</score></student><student><number>2</number><name>mary</name><score>98</score></student></students>第三章上機題1、<?xmlversion="1.0"encoding="GB2312"?><!ELEMENT學(xué)生名冊(學(xué)生+)><!ELEMENT學(xué)生(姓名,性別,年齡)><!ELEMENT姓名(#PCDATA)><!ELEMENT性別(#PCDATA)><!ELEMENT年齡(#PCDATA)><!ATTLIST學(xué)生學(xué)號ID#REQUIRED>2、<?xmlversion="1.0"encoding="UTF-8"?><NEWSPAPER><ARTICLEAUTHOR=〞&NEWSPAPER;〞EDITOR=〞&PUBLISHER;〞DATE=〞2009-6-1”EDITION=〞©RIGHT;<HEADLINE>EnvironmentProtection</HEADLINE><BYLINE>Materialwaste</BYLINE><LEAD>&PUBLISHER;</LEAD><BODY>itmeansthewasteofsubstanceorthingsfromwhichsomethingelsecanbemade.Forexample,peoplethrowawaytheusedmetalproductsandbuyanewone,sotheoldmetalwasteaccumulates.Atlast,theybecomewaste.Forexample,whenpeoplefinishreadingnewspaper,thepaperwillbecomewaste.Whentheglassisbroken,italsobecomeswaste.Nobodywantstouseabrokenwindowordrinkwithabrokenglassorwearapairofbrokenglasses.Onceitisbroken,itbecomeswaste.Therateofusingplasticsisincreasingdaybyday.Peopleuseplasticbagsbecausetheyareconvenient.Whentheygethome,theythrowtheplasticsway,payingnoattentiontotheenvironment.Themoreconvenienceplasticsbringstous,themoreplasticwasteisproduced.Thisiscalled“whitepollution〞asmostplasticsarewhite.Someofourproductsweuseeverydayaremadeofrubber,justlikethetyresandthebottompartofourshoes.It’struethatrubberplaysanimportantroleinourlife.However,italsobringsussometroublewhenithasbeenusedforalongtimeandbecomesold.Theoldtyresbecomewastebecauseitcan’tbreakdownbynature.Thisisreallyabigproblem.</BODY><NOTES>Protectingtheenvironmentisnotoneperson’sduty;itdependsonallofus.Whatcanwedo?Maybeadozen..Inaword,usewhatcanreuseasmuchaspossible.Remember,environmentprotectionisnotone’sduty!</NOTES></ARTICLE></NEWSPAPER>3、<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPENEWSPAPER[<!ELEMENTNEWSPAPER(ARTICLE+)><!ELEMENTARTICLE(HEADLINE,BYLINE,LEAD,BODY,NOTES)><!ELEMENTHEADLINE(#PCDATA)><!ELEMENTBYLINE(#PCDATA)><!ELEMENTLEAD(#PCDATA)><!ELEMENTBODY(#PCDATA)><!ELEMENTNOTES(#PCDATA)><!ATTLISTARTICLEAUTHORCDATA#REQUIRED><!ATTLISTARTICLEEDITORCDATA#IMPLIED><!ATTLISTARTICLEDATECDATA#IMPLIED><!ATTLISTARTICLEEDITIONCDATA#IMPLIED><!ENTITYNEWSPAPER"VervetLogicTimes"><!ENTITYPUBLISHER"VervetLogicPress"><!ENTITYCOPYRIGHT"Copyright1998VervetLogicPress">]><NEWSPAPER><ARTICLEAUTHOR=〞&NEWSPAPER;〞EDITOR=〞&PUBLISHER;〞DATE=〞2009-6-1”EDITION=〞©RIGHT;<HEADLINE>EnvironmentProtection</HEADLINE><BYLINE>Materialwaste</BYLINE><LEAD>&PUBLISHER;</LEAD><BODY>itmeansthewasteofsubstanceorthingsfromwhichsomethingelsecanbemade.Forexample,peoplethrowawaytheusedmetalproductsandbuyanewone,sotheoldmetalwasteaccumulates.Atlast,theybecomewaste.Forexample,whenpeoplefinishreadingnewspaper,thepaperwillbecomewaste.Whentheglassisbroken,italsobecomeswaste.Nobodywantstouseabrokenwindowordrinkwithabrokenglassorwearapairofbrokenglasses.Onceitisbroken,itbecomeswaste.Therateofusingplasticsisincreasingdaybyday.Peopleuseplasticbagsbecausetheyareconvenient.Whentheygethome,theythrowtheplasticsway,payingnoattentiontotheenvironment.Themoreconvenienceplasticsbringstous,themoreplasticwasteisproduced.Thisiscalled“whitepollution〞asmostplasticsarewhite.Someofourproductsweuseeverydayaremadeofrubber,justlikethetyresandthebottompartofourshoes.It’struethatrubberplaysanimportantroleinourlife.However,italsobringsussometroublewhenithasbeenusedforalongtimeandbecomesold.Theoldtyresbecomewastebecauseitcan’tbreakdownbynature.Thisisreallyabigproblem.</BODY><NOTES>Protectingtheenvironmentisnotoneperson’sduty;itdependsonallofus.Whatcanwedo?Maybeadozen..Inaword,usewhatcanreuseasmuchaspossible.Remember,environmentprotectionisnotone’sduty!</NOTES></ARTICLE></NEWSPAPER>第四章上機題1、<?xmlversion="1.0"encoding="UTF-8"?><xs:schemaxmlns:xs=":///2001/XMLSchema"><xs:elementname="Orders"type="ord"/><xs:complexTypename="ord"><xs:elementname="Order"><xs:complexType><xs:sequence><xs:elementname="name"type="xs:string"/><xs:elementname="number"type="numgs"/><xs:elementname="city"type="xs:string"/><xs:elementname="zip"type="zipgs"/></xs:sequence><xs:attributename="orderID"type="idgs"use="required"/><xs:attributename="orderDate"type="xs:date"use="optional"/></xs:complexType></xs:element></xs:complexType><xs:simpleTypename=“zipgs〞><xs:restrictionbase=“xs:string〞><xs:lengthvalue=“6”<xs:patternvalue=“\d{6}〞/></xs:restriction></xs:simpleType><xs:simpleTypename=“idgs〞><xs:restrictionbase=“xs:string〞><xs:lengthvalue=“4”<xs:patternvalue=“[A]{1}\d{3}〞/></xs:restriction></xs:simpleType><xs:simpleTypename=“numgs〞><xs:restrictionbase=“xs:positiveInteger〞><xs:maxInclusivevalue=“99”<xs:minInclusicevalue=“1”</xs:restriction></xs:simpleType></xs:schema>2、<?xmlversion="1.0"?><xsd:schemaxmlns:xsd=":///2001/XMLSchema"><xsd:attributename="PRODID"type="pid"/><xsd:simpleTypename="pid"><xsd:restrictionbase="xsd:string"><xsd:patternvalue="[P]{1}\d{3}"/></xsd:restriction></xsd:simpleType><xsd:elementname="PRODUCTDATA"type="prdata"/><xsd:complexTypename="prdata"><xsd:sequence><xsd:elementname="PRODUCT"> <xsd:complexType> <xsd:sequence> <xsd:elementname="PRODUCTNAME"type="xsd:string"/> <xsd:elementname="DESCRIPTION"type="xsd:string"/> <xsd:elementname="PRICE"type="xsd:positiveInteger"/> <xsd:elementname="QUANTITY"type="xsd:nonNegativeInteger"/> </xsd:sequence> <xsd:attributeref="PRODID"use="required"> </xsd:attribute> <xsd:attributename="CATEGORY"type="cat"use="required"/> </xsd:complexType> </xsd:element></xsd:sequence></xsd:complexType><xsd:si9mpleTypename="cat"><xsd:restrictionbase="xsd:string"><xsd:enumerationvalue="Toy"/><xsd:enumerationvalue="Book"/></xsd:restriction></xsd:simpleType></xsd:schema>3、<?xmlversion="1.0"encoding="UTF-8"?><xs:schemaxmlns:xs=":///2001/XMLSchema"><xs:elementname="PURCHASEORDER"type="pur"/><xs:complexTypename="pur"><xs:elementname="Order"><xs:complexType><xs:sequence><xs:elementname="DATE"type="xs:date"/><xs:elementname="PRODUCTNAME"type="xs:string"/><xs:elementname="SUPPLIERNAME"type="xs:string"/><xs:elementname="ADDRESS"type="xs:string"/><xs:elementname="QUANTITY"type="xs:positiveInteger"/><xs:elementname="PRICEPERQTY"type="xs:positiveInteger"/></xs:sequence></xs:complexType></xs:element><xs:attributename="ORDERID"type="xs:string"use="required"/><xs:attributename="PRODID"type="xs:string"use="optional"/></xs:complexType></xs:schema>第五章上機題1、1〕、<?xmlversion="1.0"encoding="gb2312"?><xsl:stylesheetversion="1.0"xmlns:xsl=":///1999/XSL/Transform"><xsl:templatematch="/"><html><head><title>訂單信息</title></head><body><h2align="center">訂單</h2> <tableborder="1"cellpadding="0"align="center"> <tr><th>名稱</th><th>數(shù)量</th><th>城市</th><th></th></tr> <xsl:for-eachselect="/Orders/Order"><tr><td><xsl:value-ofselect="name"/></td><td><xsl:value-ofselect="number"/></td><td><xsl:value-ofselect="city"/></td><td><xsl:value-ofselect="zip"/></td><//tr> </xsl:for-each> </table></body></html></xsl:template></xsl:stylesheet>2〕、<?xmlversion="1.0"encoding="gb2312"?><xsl:stylesheetversion="1.0"xmlns:xsl=":///1999/XSL/Transform"><xsl:templatematch="/"><html><head><title>訂單信息</title></head><body><h2align="center">訂單</h2> <tableborder="1"cellpadding="0"align="center"> <tr><th>名稱</th><th>數(shù)量</th><th>城市</th><th></th></tr><xsl:iftest="http://Order[@orderID='A002']"><tr><td><xsl:value-ofselect="name"/></td><td><xsl:value-ofselect="number"/></td><td><xsl:value-ofselect="city"/></td><td><xsl:value-ofselect="zip"/></td></tr></xsl:if> </table></body></html></xsl:template></xsl:stylesheet>3〕、<?xmlversion="1.0"encoding="gb2312"?><xsl:stylesheetversion="1.0"xmlns:xsl=":///1999/XSL/Transform"><xsl:templatematch="/"><html><head><title>訂單信息</title></head><body><h2align="center">訂單</h2> <tableborder="1"cellpadding="0"align="center"> <tr><th>名稱</th><th>數(shù)量</th><th>城市</th><th></th></tr><xsl:iftest="city[.='北京']"><tr><td><xsl:value-ofselect="name"/></td><td><xsl:value-ofselect="number"/></td><td><xsl:value-ofselect="city"/></td><td><xsl:value-ofselect="zip"/></td></tr></xsl:if> </table></body></html></xsl:template></xsl:stylesheet>2、PRODUCTNAME{font-family:Arial;font-size:20pt;font-weight:bold;color:red;display:block;padding-top:6pt;padding-bottom:6pt}PRICE,DESCRIPTION,QUANTITY{font-family:Arial;font-size:10pt;color:green;display:block;padding-top:2pt;padding-bottom:2pt}3、<?xmlversion="1.0"?><xsl:stylesheetxmlns:xsl=":///1999/XSL/Transform"version="1.0"><xsl:templatematch="/"><xsl:for-eachselect="PRODUCTDATA/PRODUCT"><xsl:choose><xsl:whentest="PRODUCTNAME"><fontcolor="red">產(chǎn)品名:<xsl:value-ofselect="PRODUCTNAME"/><BR/></font></xsl:when><xsl:otherwise><fontcolor="green">描述:<xsl:value-ofselect="DESCRIPTION"/><BR/>價格:<xsl:value-ofselect="PRICE"/><BR/>現(xiàn)有量:<xsl:value-ofselect="QUANTITY"/><BR/><BR/></font></xsl:otherwise></xsl:choose></xsl:for-each></xsl:template></xsl:stylesheet>第六章上機題1、翻開IE瀏覽器,并在地址欄中輸入URL查詢字符串:://localhost/XMLTest?sql=select+*from+student+for+xml+RAW&root=root://localhost/XMLTest?sql=select+*from+student+for+xml+Auto&root=student2、將數(shù)據(jù)“bin〞、“female〞、“30”DECLARE@docvarchar(1000)DECLARE@idocintSET@doc='<ROOT><student><id>5</id><name>bin</name><sex>female</sex><age>30</age></student></ROOT>'execsp_xml_preparedocument@idocoutput,@docselect*fromopenxml(@idoc,'/ROOT/student',2)with(idint,namevarchar(40),sexvarchar(20),ageint)insertstudentselect*fromopenxml(@idoc,'/ROOT/student',2)withstudentexecsp_xml_removedocument@idoc3、將數(shù)據(jù)“bin〞、“female〞、“30”USEschoolDECLARE@docvarchar(1000)DECLARE@idocintSET@doc='<ROOT><studentid="6"name="bin"sex="female"age="30"></student></ROOT>'execsp_xml_preparedocument@idocoutput,@docselect*fromopenxml(@idoc,'/ROOT/student',1)with(idint,namevarchar(40),sexvarchar(20),ageint)insertstudentselect*fromopenxml(@idoc,'/ROOT/student')withstudentexecsp_xml_removedocument@idoc第七章上機題1、<GOTOxlink:type=〞simple〞xlink:href=〞〞xlink:title=〞Address〞xlink:show=〞replace〞xlink:actuate=〞onRequest〞>thisisaslinkedelement</GOTO>2、<GOTOxlink:type=〞simple〞xlink:href=〞〞xlink:title=〞Address〞xlink:show=〞new〞xlink:actuate=〞onRequest〞>thisisaslinkedelement</GOTO>3、<?xmlversion="1.0"encoding="gb2312"?><library><bookname="C#技術(shù)內(nèi)幕"id="1101"><coverurl=":///book/cover01.jpg"/><press>機械工業(yè)出版社</press><description>這是一本C#編程語言的指南和參考書。C#是一種嶄新的面向?qū)ο蟮木幊陶Z言。它強調(diào)以組件為根底的軟件開發(fā)方法。</description></book><bookname="時間簡史"id="5105"><coverurl=":///book/cover51.jpg"/><press>湖南科學(xué)技術(shù)出版社</press><description>《時間簡史》以最通俗的語言,對一些最古老的問題做了闡述,向人們介紹了什么是宇宙論,以及宇宙論最新的開展?fàn)顩r。</description></book><bookname="ASP.NET技術(shù)內(nèi)幕"id="1102"><coverurl=":///book/cover01.jpg"/><press>機械工業(yè)出版社</press><description>這是一本ASP.NET編程語言的指南和參考書。</description></book><bookname="Java導(dǎo)論"id="1105"><coverurl=":///book/cover51.jpg"/><press>湖南科學(xué)技術(shù)出版社</press><description>這是一本Java編程語言的指南和參考書。</description></book><bookname="JavaME大全"id="1105"><coverurl=":///book/cover51.jpg"/><press>湖南科學(xué)技術(shù)出版社</press><description>這是一本JavaME編程語言的指南和參考書。</description></book></library><?xmlversion="1.0"?><browsexmlns:xlink=":///1999/xlink"><bookxlink:type="simple"xlink:href=":///books.xml#xpointer(id('1102'))"><descriptionxlink:type="simple"xlink:href=":///book/cover01.jpg">學(xué)習(xí)ASP.NET的書籍</description></book></browse>第八章填空題:1、文檔對象模型2、DOM解析器3、節(jié)點4、DOMDocument5、IXMLDOMNode選擇題:1、ABCD2、B3、D4、C5、A簡答題:1、答:最常見的節(jié)點類型有:(1)、元素:元素是XML的根本構(gòu)件。元素可以有其它元素、文本節(jié)點或兩者兼有來作為其子節(jié)點。元素節(jié)點還是可以有屬性的唯一類型的節(jié)點。(2)、屬性:屬性節(jié)點包含關(guān)于元素節(jié)點的信息,但實際上,不認(rèn)為它是元素的子節(jié)點(3)、文本:文本節(jié)點是:文本。它可以包含許多信息或僅僅是空白。(4)、文檔〔根節(jié)點〕:文檔節(jié)點是整個文檔中所有其它節(jié)點的父節(jié)點。2、答:使用XMLDOM,可以讓我們遍歷、讀取和操縱XML文檔的結(jié)構(gòu)和內(nèi)容,其主要對象和方法有:(1)、DOMDocument對象:描述全部文檔映射表,它包括文檔所有信息。對開發(fā)人員來說,最重要的編程對象是DOMDocument。DOMDocument對象通過暴露的屬性和方法來允許瀏覽、查詢和修改XML文檔的內(nèi)容和結(jié)構(gòu)(2)、IXMLDOMNode對象:描述文檔里的節(jié)點,此節(jié)點可以是元素、屬性、處理指令、文本或其他存儲在XML文檔里的信息。(3)、IXMLDOMNodeList對象:描述節(jié)點對象的集合,可以使用該對象遍歷這個集合,可以用數(shù)值訪問。(4)、IXMLDOMParseError對象:用來驗證XML文檔對于DTD、XSD的正確性。第九章填空題:1、DOMSAX2、Document3、startDocument4、XMLOutPutter5、Element選擇題:1、B2、A3、A4、B5、D上機題1、(1) importjavax.xml.parsers.*; importorg.w3c.dom.*; publicclassdom{ publicstaticvoidmain(Stringargs[]){ try{ DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbuilder=factory.newDocumentBuilder(); Documentdoc=builder.parse("order.xml"); NodeListnl=doc.getElementsByTagName("shipTo"); for(inti=0;i<nl.getLength();i++){ Elementnode=(Element)nl.item(i); System.out.print("name:"); System.out.println(node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()); System.out.print("street:"); System.out.println(node.getElementsByTagName("street").item(0).getFirstChild().getNodeValue()); System.out.print("city:"); System.out.println(node.getElementsByTagName("city").item(0).getFirstChild().getNodeValue()); System.out.print("state:"); System.out.println(node.getElementsByTagName("state").item(0).getFirstChild().getNodeValue()); System.out.print("zip:"); System.out.println(node.getElementsByTagName("zip").item(0).getFirstChild().getNodeValue()); System.out.println(); }NodeListn2=doc.getElementsByTagName("billTo"); for(inti=0;i<n2.getLength();i++){ Elementnode=(Element)n2.item(i); System.out.print("name:"); System.out.println(node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()); System.out.print("street:"); System.out.println(node.getElementsByTagName("street").item(0).getFirstChild().getNodeValue()); System.out.print("city:"); System.out.println(node.getElementsByTagName("city").item(0).getFirstChild().getNodeValue()); System.out.print("state:"); System.out.println(node.getElementsByTagName("state").item(0).getFirstChild().getNodeValue()); System.out.print("zip:"); System.out.println(node.getElementsByTagName("zip").item(0).getFirstChild().getNodeValue()); System.out.println(); }NodeListn3=doc.getElementsByTagName("item"); for(inti=0;i<n3.getLength();i++){ Elementnode=(Element)n3.item(i); System.out.print("productName:"); System.out.println(node.getElementsByTagName("productName").item(0).getFirstChild().getNodeValue()); System.out.print("quantity:"); System.out.println(node.getElementsByTagName("quantity").item(0).getFirstChild().getNodeValue()); System.out.print("USPrice:"); System.out.println(node.getElementsByTagName("USPrice").item(0).getFirstChild().getNodeValue()); System.out.print("comment:"); System.out.println(node.getElementsByTagName("comment").item(0).getFirstChild().getNodeValue()); System.out.println(); } }catch(Exceptione){ e.printStackTrace(); } } }(2) importjavax.xml.parsers.*; importorg.w3c.dom.*; importjava.io.*; importorg.apache.crimson.tree.*; publicclassInsertElement{ publicstaticvoidmain(Stringargs[]){ Elementitems=null; Elementitem=null;ElementproductName=null; Elementquantity=null;ElementUSPrice=null; ElementshipDate=null; try{ DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbuilder=factory.newDocumentBuilder(); Documentdoc=builder.parse("order.xml"); NodeListnl=doc.getElementsByTagName("items"); items=(Element)nl.item(0); item=doc.createElement("item"); item.setAttribute("partNum","926-AA"); productName=doc.createElement("productName"); productName.appendChild(doc.createTextNode("BabyMonitor")); item.appendChild(productName); quantity=doc.createElement("quantity"); quantity.appendChild(doc.createTextNode("1")); item.appendChild(quantity); USPrice=doc.createElement("USPrice"); USPrice.appendChild(doc.createTextNode("39.98")); item.appendChild(USPrice); shipDate=doc.createElement("shipDate"); shipDate.appendChild(doc.createTextNode("1999-05-21")); item.appendChild(shipDate); items.appendChild(item); ((XmlDocument)doc).write(newFileOutputStream("test1.xml")); }catch(Exceptione){ e.printStackTrace(); } } }2、importjava.io.IOException;importjavax.xml.parsers.*;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;publicclassSAXtoMyxml{publicstaticvoidmain(String[]args)throwsParserConfigurationException,SAXException,IOException{//通過工廠獲得SAX解析器SAXParserFactorysf=SAXParserFactory.newInstance();SAXParsersax=sf.newSAXParser();//解析器//通過解析器解析xml文件sax.parse("order.xml",newSAXHander());//使用自定義的監(jiān)聽器}}//自定義sax解析監(jiān)聽器classSAXHanderextendsDefaultHandler{publicvoidstartDocument()throwsSAXException{System.out.println("文檔開始");}publicvoidstartElement(Stringuri,StringlocalName,StringqName,Attributesattributes)throwsSAXException{System.out.println("元素開始"+qName);}publicvoidcharacters(char[]ch,intstart,intlength)throwsSAXException{Stringtext=newString(ch,start,length);//去掉xml文件中的空格節(jié)點if(text.trim().equals("")){return;}System.out.println("文本內(nèi)容"+text);}publicvoidendElement(Stringuri,StringlocalName,StringqName)throwsSAXException{System.out.println("元素結(jié)束"+qName);}publicvoidendDocument()throwsSAXException{System.out.println("文檔結(jié)束");}}3、 importorg.jdom.*; importorg.jdom.output.*; importjava.io.*; publicclassWriteXML{ publicvoidBuildXML()throwsException{ Elementroot,student,number,name,age; root=newElement("student-info"); student=newElement("student"); number=newElement("number"); name=newElement("name"); age=newElement("age"); Documentdoc=newDocument(root); number.setText("001"); name.setText("lnman"); age.setText("24"); student.addContent(number); student.addContent(name); student.addContent(age); root.addContent(student); Formatformat=Format.getCompactFormat(); format.setEncoding("gb2312"); format.setIndent(""); XMLOutputterXMLOut=newXMLOutputter(format); XMLOut.output(doc,newFileOutputStream("studentinfo.xml")); } publicstaticvoidmain(String[]args)throwsException{ WriteXMLw=newWriteXML(); System.out.println("正在生成XML文檔....."); w.BuildXML(); System.out.println("完成"); } }第十章填空題:1、流模式DOM2、XmlReader3、XmlDocument4、WriteXml5、XmlNode選擇題:1、A2、D3、A4、A5、A上機題1、假設(shè)XML文檔的文件名為order.xml,位于C:\目錄下。1〕、第一步:在.NET集成開發(fā)環(huán)境中創(chuàng)立一個名為ReadXmlDemo的控制臺應(yīng)用程序;第二步:導(dǎo)入命名空間usingSystem.Xml;第三步:讀取數(shù)據(jù)完整的實例代碼如下所示:usingSystem;usingSystem.Xml;namespaceReadXmlDemo2{ classClass1 { [STAThread] staticvoidMain(string[]args) { intws=0; intpi=0; intdc=0; intcc=0; intac=0; intet=0; intel=0; intxd=0; XmlTextReadertextReader=newXmlTextReader("C:\\order.xml"); while(textReader.Read()) { XmlNodeTypenType=textReader.NodeType; if(nType==XmlNodeType.XmlDeclaration) {Console.WriteLine("Declaration:"+textReader.Name.ToString()); xd=xd+1; } if(nType==XmlNodeType.Comment) { Console.WriteLine("Comment:"+textReader.Name.ToString()); cc=cc+1; } if(nType==XmlNodeType.Attribute) { Console.WriteLine("Attribute:"+textReader.Name.ToString()); ac=ac+1; } if(nType==XmlNodeType.Element) { Console.WriteLine("Element:"+textReader.Name.ToString()); el=el+1; } if(nType==XmlNodeType.Entity) { Console.WriteLine("Entity:"+textReader.Name.ToString()); et=et+1; } if(nType==XmlNodeType.ProcessingInstruction) { Console.WriteLine("ProcessInstruction:"+textReader.Name.ToString()); pi=pi+1; }if(nType==XmlNodeType.DocumentType) { Console.WriteLine("DocumentType:"+textReader.Name.ToString()); dc=dc+1; } if(nType==XmlNodeType.Whitespace) { Console.WriteLine("WhiteSpace:"+textReader.Name.ToString()); ws=ws+1; } } Console.WriteLine("TotalComments:"+cc.ToString()); Console.WriteLine("TotalAttributes:"+ac.ToString()); Console.WriteLine("TotalElements:"+el.ToString()); Console.WriteLine("TotalEntity:"+et.ToString()); Console.WriteLine("TotalProcessInstructions:"+pi.ToString()); Console.WriteLine("TotalDeclaration:"+xd.ToString()); Console.WriteLine("TotalDocumentType:"+dc.ToString()); Console.WriteLine("TotalWhiteSpaces:"+ws.ToString()); } }}2〕、首先將要用到的文件order.xml放入工程所在文件夾目錄下的bin\Debug目錄下;接著聲明窗體級的數(shù)據(jù)集對象,代碼如下: DataSetds=newDataSet();然后,編寫各控件相關(guān)的事件觸發(fā)程序代碼:顯示“讀取xml文檔〞的按鈕的單擊事件觸發(fā)程序代碼如下: privatevoidbtnReadXML_Click(objectsender,System.EventArgse) { ds.ReadXml("order.xml");dataGrid1.DataSource=ds.Tables[0]; }顯示“將數(shù)據(jù)集中的架構(gòu)寫入文件〞的按鈕的單擊事件觸發(fā)程序代碼如下: privatevoidbtnWriteXmlSchema_Click(objectsender,System.EventArgse) { ds.WriteXmlSchema("order.xsd"); }2、假設(shè)XML文檔的文件名為book.xml第一步:在.NET集成開發(fā)環(huán)境中創(chuàng)立一個名為NavigateXmlDoc的WINDOWS應(yīng)用程序;第二步:將要用到的文件book.xml放入本工程所在文件夾目錄下;第三步:設(shè)計窗體的外觀如圖10-33所示:第四步:導(dǎo)入命名空間usingSystem.Xml;usingSystem.IO;第五步:編寫各控件相關(guān)的事件觸發(fā)程序代碼,使得代碼窗口中的內(nèi)容如下所示:usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.Xml;usingSystem.IO;namespaceNavigateXmlDoc{ ///<summary> ///SummarydescriptionforForm1. ///</summary> { privateSystem.Windows.Forms.GroupBoxgroupBox4; privateSystem.Windows.Forms.Buttonbtn_FindNode; privateSystem.Windows.Forms.TextBoxtxtElementValue; privateSystem.Windows.Forms.GroupBoxgroupBox5; privateSystem.Windows.Forms.ButtonbtnDocElement; privateSystem.Windows.Forms.ButtonbtnFirstChild; privateSystem.Windows.Forms.ButtonbtnLastChild; privateSystem.Windows.Forms.ButtonbtnNextSib; privateSystem.Windows.Forms.ButtonbtnParentNode; privateSystem.Windows.Forms.ButtonbtnPrevSib; privateSystem.Windows.Forms.GroupBoxgroupBox2; privateSystem.Windows.Forms.TextBoxtxtResults; privateSystem.Windows.Forms.ButtonbtnClose; privateSystem.Windows.Forms.GroupBoxgroupBox1; privateSystem.Windows.Forms.ButtonbtnMinus; privateSystem.Windows.Forms.ButtonbtnPlus; privateSystem.Windows.Forms.ButtonbtnLoad; privateSystem.Windows.Forms.TextBoxtxtDocument; privateSystem.Windows.Forms.GroupBoxgroupBox3; privateSystem.Windows.Forms.ButtonbtnGetByName; privateSystem.Windows.Forms.TextBoxtxtElement; ///<summary> ///Requireddesignervariable. ///</summary> privateSystem.ComponentModel.Containercomponents=null; privateXmlDocumentdoc; privateXmlNodecurrentNode; publicForm1() { // //RequiredforWindowsFormDesignersupport // InitializeComponent(); // //TODO:AddanyconstructorcodeafterInitializeComponentcall // } ///<summary> ///Cleanupanyresourcesbeingused. ///</summary> protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); }//此處省略的為窗體設(shè)計器自動生成的代碼 ///<summary> ///Themainentrypointfortheapplication. ///</summary> [STAThread] staticvoidMain() { Application.Run(newForm1()); } privatevoidbtnPlus_Click(objectsender,System.EventArgse) { if(txtDocument.Font.SizeInPoints<32) { txtDocument.Font=newFont("MicrosoftSansSerif",txtDocument.Font.Size+2); } } privatevoidbtnMinus_Click(objectsender,System.EventArgse) { if(txtDocument.Font.SizeInPoints>8) { txtDocument.Font=newFont("MicrosoftSansSerif",txtDocument.Font.Size-2); } } privatevoidbtnLoad_Click(objectsender,System.EventArgse) { StringfileName=@"..\..\book.xml"; StreamReaderreader=newStreamReader(fileName); txtDocument.Text=reader.ReadToEnd(); doc=newXmlDocument(); doc.PreserveWhitespace=false; doc.LoadXml(txtDocument.Text); } privatevoidbtnDocElement_Click(objectsender,System.EventArgse) { XmlNodelastNode; lastNode=currentNode; try { currentNode=doc.DocumentElement; txtResults.Text=currentNode.OuterXml; } catch(Exceptione1) { MessageBox.Show("沒有匹配的節(jié)點"+e1.ToString(),"導(dǎo)航出錯", MessageBoxButtons.OK,MessageBoxIcon.Asterisk); currentNode=lastNode; } } privatevoidbtnFirstChild_Click(objectsender,System.EventArgse) { XmlNodela
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電力施工課件教學(xué)課件
- 2024年度乙方為甲方提供財務(wù)咨詢服務(wù)合同
- 2024年度供應(yīng)鏈融資合同融資額度與還款方式說明
- 2024醫(yī)療器械公司與研發(fā)團(tuán)隊合作協(xié)議
- 2024年度技術(shù)服務(wù)與授權(quán)合同
- 2024婚姻擔(dān)保協(xié)議合同
- 2024建筑的裝飾合同書范本
- 2024年度版權(quán)出租合同詳細(xì)條款及其標(biāo)的
- 2024年居住房屋買賣合同
- 畫小雞課件教學(xué)課件
- 2024年二手物品寄售合同
- 2023年遼陽宏偉區(qū)龍鼎山社區(qū)衛(wèi)生服務(wù)中心招聘工作人員考試真題
- 三年級數(shù)學(xué)(上)計算題專項練習(xí)附答案集錦
- 歷史期中復(fù)習(xí)課件七年級上冊復(fù)習(xí)課件(部編版2024)
- 7.2.2 先天性行為和學(xué)習(xí)行為練習(xí) 同步練習(xí)
- 2024-2025學(xué)年八年級物理上冊 4.2光的反射說課稿(新版)新人教版
- 《現(xiàn)代管理原理》章節(jié)測試參考答案
- 電子元器件有效貯存期、超期復(fù)驗及裝機前的篩選要求
- 2024秋期國家開放大學(xué)??啤陡叩葦?shù)學(xué)基礎(chǔ)》一平臺在線形考(形考任務(wù)一至四)試題及答案
- 懷感恩與愛同行 主題班會課件
- 停車收費系統(tǒng)購買合同范本
評論
0/150
提交評論