Professional C# Third Edition phần 7 ppsx

140 314 0
Professional C# Third Edition phần 7 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

newTitle.InnerText=”The Case of the Missing Cookie”; newBook.AppendChild(newTitle); //create new author element XmlElement newAuthor=doc.CreateElement(“author”); newBook.AppendChild(newAuthor); //create new name element XmlElement newName=doc.CreateElement(“name”); newName.InnerText=”C. Monster”; newAuthor.AppendChild(newName); //create new price element XmlElement newPrice=doc.CreateElement(“price”); newPrice.InnerText=”9.95”; newBook.AppendChild(newPrice); //add to the current document doc.DocumentElement.AppendChild(newBook); //write out the doc to disk XmlTextWriter tr=new XmlTextWriter(“ \\ \\ \\booksEdit.xml”,null); tr.Formatting=Formatting.Indented; doc.WriteContentTo(tr); tr.Close(); //load listBox1 with all of the titles, including new one XmlNodeList nodeLst=doc.GetElementsByTagName(“title”); foreach(XmlNode node in nodeLst) listBox1.Items.Add(node.InnerText); } After executing this code, you end up with the same functionality as in the previous example, but there is one additional book in the list box, The Case of the Missing Cookie (a soon-to-be classic). Clicking on the cookie caper title will show all of the same info as the other titles. Breaking down the code, we can see that this is actually a fairly simple process. The first thing that we do is create a new book element: XmlElement newBook = doc.CreateElement(“book”); CreateElement() has three overloads that allow you to specify: ❑ The element name ❑ The name and namespace URI ❑ The prefix, localname, and namespace Once the element is created we need to add attributes: newBook.SetAttribute(“genre”,”Mystery”); newBook.SetAttribute(“publicationdate”,”2001”); newBook.SetAttribute(“ISBN”,”123456789”); Now that we have the attributes created, we need to add the other elements of a book: XmlElement newTitle = doc.CreateElement(“title”); newTitle.InnerText = “The Case of the Missing Cookie”; newBook.AppendChild(newTitle); 800 Chapter 23 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 800 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Once again we create a new XmlElement-based object (newTitle). Then we set the InnerText property to the title of our new classic, and append the element as a child to the book element. We repeat this for the rest of the elements in this book element. Note that we add the name element as a child to the author element. This will give us the proper nesting relationship as in the other book elements. Finally, we append the newBook element to the doc.DocumentElement node. This is the same level as all of the other book elements. We have now updated an existing document with a new element. The last thing to do is to write the new XML document to disk. In this example we create a new XmlTextWriter, and pass it to the WriteContentTo() method. WriteContentTo() and WriteTo() both take an XmlTextWriter as a parameter. WriteContentTo() saves the current node and all of its children to the XmlTextWriter, whereas WriteTo() just saves the current node. Because doc is an XmlDocument-based object, it represents the entire document and so that is what is saved. We could also use the Save() method. It will always save the entire document. Save() has four overloads. You can specify a string with the file name and path, a Stream-based object, a TextWriter-based object, or an XmlWriter-based object. We also call the Close() method on XmlTextWriter to flush the internal buffers and close the file. Figure 23-6 shows what we get when we run this example. Notice the new entry at the bottom of the list: Figure 23-6 If we wanted to create a document from scratch, we could use the XmlTextWriter, which we saw in action earlier in the chapter. We can also use XmlDocument. Why would you use one in preference to the other? If the data that you want streamed to XML is available and ready to write, then the XmlTextWriter class would be the best choice. However, if you need to build the XML document a lit- tle at a time, inserting nodes into various places, then creating the document with XmlDocument might be the better choice. We can accomplish this by changing the following line: doc.Load(“ \\ \\ \\books.xml”); to this code (example DOMSample4): 801 Manipulating XML 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 801 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com //create the declaration section XmlDeclaration newDec = doc.CreateXmlDeclaration(“1.0”,null,null); doc.AppendChild(newDec); //create the new root element XmlElement newRoot = doc.CreateElement(“newBookstore”); doc.AppendChild(newRoot); First, we create a new XmlDeclaration. The parameters are the version (always 1.0 for now), the encoding, and the standalone flag. The encoding parameter should be set to a string that is part of the System.Text.Encoding class if null isn’t used. (null defaults to UTF-8). The standalone flag can be either yes, no, or null. If it is null then the attribute is not used and will not be included in the document. The next element that is created will become the DocumentElement. In this case, we called it newBookstore so that you can see the difference. The rest of the code is the same as in the previous example and works in the same way. This is booksEdit.xml, which is generated from the code: <?xml version=”1.0”?> <newBookstore> <book genre=”Mystery” publicationdate=”2001” ISBN=”123456789”> <title>The Case of the Missing Cookie</title> <author> <name>C. Monster</name> </author> <price>9.95</price> </book> </newBookstore> We have not exhausted our exploration of the XmlDocument class, or of the other classes that help to cre- ate the DOM model in .NET. However, you how powerful and flexible the DOM implementation in .NET offers. You will want to use the XmlDocument class when you want to have random access to the document, or the XmlReader-based classes when you want a streaming type model instead. Remember that there is a cost for the flexibility of the XmlNode-based XmlDocument class—memory requirements are higher and the performance of reading the document is not as good as using XmlReader. So think carefully about which method best fits your needs. Using XPath and XSLT in .NET In this section, we discuss support for XPath and XSL Transforms (XSLT) in.NET Framework. XPath sup- port is provided through the System.Xml.XPath namespace, and XSLT through the System.Xml.Xsl namespace. The reason that we are looking at them together is that the XPathNavigator class of the System.Xml.XPath namespace provides a very performance-oriented way of performing XSL Transforms in .NET. XPath is the query language for XML. You would use XPath to select a subset of elements based on ele- ment text values or perhaps based on attribute values. XSLT is used to transform a base document into another document of different structure or type. We will first look at System.Xml.XPath and then discuss how it is used to feed the System.Xml.Xsl classes. 802 Chapter 23 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 802 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com The System.Xml.XPath Namespace The System.Xml.XPath namespace is built for speed. It provides a read-only view of your XML docu- ments, so there are no editing capabilities. Classes in this namespace are built to do fast iteration and selections on the XML document in a cursor fashion. Here is a table that lists the key classes in System.Xml.XPath, and gives a short description of the pur- pose of each class: Class Name Description XPathDocument Provides a view of the entire XML document. Read-only. XPathNavigator Provides the navigation capabilities to an XPathDocument. XPathNodeIterator Provides iteration capabilities to a node set. XPath equivalent to a nodeset in Xpath. XPathExpression Represents a compiled XPath expression. Used by SelectNodes, SelectSingleNodes, Evaluate, and Matches. XPathException Is an XPath exception class. XPathDocument XPathDocument doesn’t offer any of the functionality of the XmlDocument class. If you need editing capabilities, then XmlDocument is the way to go; if you’re using ADO.NET, go with XmlDataDocument (discussed later in this chapter). However, if speed is of concern, then use XPathDocument as your store. It has four overloads allowing you to open an XML document from a file and path string, a TextReader object, an XmlReader object, or a Stream-based object. XPathNavigator XPathNavigator contains all of the methods for moving and selecting elements that you need. The fol- lowing table lists some of the “move” methods defined in this class. Method Name Description MoveTo() Takes XPathNavigator as a parameter. Moves the current posi- tion to be the same as that passed in to XPathNavigator. MoveToAttribute() Moves to the named attribute. Takes the attribute name and namespace as parameters. MoveToFirstAttribute() Moves to the first attribute in the current element. Returns true if successful. MoveToNextAttribute() Moves to the next attribute in the current element. Returns true if successful. MoveToFirst() Moves to the first sibling in the current node. Returns true if suc- cessful; otherwise it returns false. Table continued on following page 803 Manipulating XML 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 803 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Method Name Description MoveToLast() Moves to the last sibling in the current node. Returns true if successful. MoveToNext() Moves to the next sibling in the current node. Returns true if successful. MoveToPrevious() Moves to the previous sibling in the current node. Returns true if successful. MoveToFirstChild() Moves to the first child of the current element. Returns true if successful. MoveToId() Moves to the element with the ID supplied as a parameter. There needs to be a schema for the document, and the data type for the element must be of type ID. MoveToParent() Moves to the parent of the current node. Returns true if successful. MoveToRoot() Moves to the root node of the document. There are several Select() methods for selecting a subset of nodes to work with. All of these Select() methods return an XPathNodeIterator object. There are also SelectAncestors() and SelectChildren() methods that you can use. Both return an XPathNodeIterator object. While Select() takes an XPath expression as a parameter, the other select methods take XPathNodeType as a parameter. You can extend XPathNavigator to use such things as the file system or registry as the store instead of XPathDocument. XPathNodeIterator XPathNodeIterator can be thought of as the equivalent of a NodeList or a NodeSet in XPath. This object has three properties and two methods: ❑ Clone—Creates a new copy of itself ❑ Count—Number of nodes in the XPathNodeIterator object ❑ Current—Returns an XPathNavigator pointing to the current node ❑ CurrentPosition()—Returns an integer with the current position ❑ MoveNext()—Moves to the next node that matches the XPath expression that created the XpathNodeIterator Using classes from the XPath namespace The best way to see how these classes are used is to look at some code that iterates through the books.xml document. This will allow you to see how the navigation works. In order to use the examples, we first add a reference to the System.Xml.Xsl and System.Xml.XPath namespaces: 804 Chapter 23 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 804 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com using System.Xml.XPath; using System.Xml.Xsl; For this example we are using the file booksxpath.xml. It is similar to the books.xml that we have been using previously, except there are a couple of extra books added. Here’s the form code, which can be found in the XPathXSLSample1 folder: private void button1_Click(object sender, System.EventArgs e) { //modify to match your path structure XPathDocument doc=new XPathDocument(“ \\ \\ \\booksxpath.xml”); //create the XPath navigator XPathNavigator nav=doc.CreateNavigator(); //create the XPathNodeIterator of book nodes // that have genre attribute value of novel XPathNodeIterator iter=nav.Select(“/bookstore/book[@genre=’novel’]”); while(iter.MoveNext()) { LoadBook(iter.Current); } } private void LoadBook(XPathNavigator lstNav) { //We are passed an XPathNavigator of a particular book node //we will select all of the descendents and //load the list box with the names and values XPathNodeIterator iterBook=lstNav.SelectDescendants (XPathNodeType.Element,false); while(iterBook.MoveNext()) listBox1.Items.Add(iterBook.Current.Name + “: “ + iterBook.Current.Value); } The first thing we do in the button1_Click() method is to create the XPathDocument (called doc), passing in the file and path string of the document we want opened. The next line is where the XPathNavigator is created: XPathNavigator nav = doc.CreateNavigator(); In the example you can see that we use the Select() method to retrieve a set of nodes that all have novel as the value of the genre attribute. We then use the MoveNext() method to iterate through all of the novels in the book list. To load the data into the list box, we use the XPathNodeIterator.Current property. This creates a new XPathNavigator object based on just the node that the XPathNodeIterator is pointing to. In this case, we are creating an XPathNavigator for one book node in the document. The LoadBook() method takes this XPathNavigator and creates another XPathNodeIterator by issuing another type of select method, the SelectDescendants() method. This gives us an XPathNodeIterator of all of the child nodes and children of the child nodes of the book node that we passed to the LoadBook() method. 805 Manipulating XML 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 805 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Then we do another MoveNext() loop on the XPathNodeIterator and load the list box with the ele- ment names and element values. Figure 23-7 shows what the screen looks like after running the code. Note that novels are the only books listed now. Figure 23-7 What if we wanted to add up the cost of these books? XPathNavigator includes the Evaluate() method for just this reason. Evaluate() has three overloads. The first one contains a string that is the XPath function call. The second overload uses the XPathExpression object as a parameter, and the third uses XPathExpression and an XPathNodeIterator as parameters. The changes are highlighted below (this version of the code can be found in XPathXSLSample2): private void button1_Click(object sender, System.EventArgs e) { //modify to match your path structure XPathDocument doc = new XPathDocument(“ \\ \\ \\booksxpath.XML”); //create the XPath navigator XPathNavigator nav = doc.CreateNavigator(); //create the XPathNodeIterator of book nodes // that have genre attribute value of novel XPathNodeIterator iter = nav.Select(“/bookstore/book[@genre=’novel’]”); while(iter.MoveNext()) { LoadBook(iter.Current.Clone()); } //add a break line and calculate the sum listBox1.Items.Add(“========================”); listBox1.Items.Add(“Total Cost = “ + nav.Evaluate(“sum(/bookstore/book[@genre=’novel’]/price)”)); } This time, we see the total cost of the books evaluated in the list box (see Figure 23-8). 806 Chapter 23 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 806 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 23-8 The System.Xml.Xsl Namespace The System.Xml.Xsl namespace contains the classes that .NET Framework uses to support XSL Transforms. The contents of this namespace are available to any store whose classes implement the IXPathNavigable interface. In.NET Framework that would currently include XmlDocument, XmlDataDocument, and XPathDocument. Again, just as with XPath, use the store that makes the most sense. If you plan to create a custom store, such as one using the file system, and you want to be able to do transforms, be sure to implement the IXPathNavigable interface in your class. XSLT is based on a streaming pull model. Because of this, you can chain several transforms together. You could even apply a custom reader between transforms if needed. This allows a great deal of flexibility in design. Transforming XML The first example we will look at takes the books.xml document and transforms it into a simple HTML document for display using the XSLT file books.xsl. (This code can be found in the XPathXSLSample3 folder.) We will need to add the following using statements: using System.IO; using System.Xml.Xsl; using System.Xml.XPath; Here is the code to perform the transform: private void button1_Click(object sender, System.EventArgs e) { //create the new XPathDocument XPathDocument doc = new XPathDocument(“ \\ \\ \\booksxpath.xml”); //create a new XslTransForm XslTransform transForm = new XslTransform(); transForm.Load(“ \\ \\ \\books.xsl”); 807 Manipulating XML 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 807 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com //this FileStream will be our output FileStream fs=new FileStream(“ \\ \\ \\booklist.html”, FileMode.Create); //Create the navigator XPathNavigator nav = doc.CreateNavigator(); //Do the transform. The output file is created here transForm.Transform(nav, null, fs, null); } A transform doesn’t get any simpler than this. We create an XPathDocument-based object and an XslTransform-based object. We load the booksxpath.xml file into the XPathDocument object, and books.xsl file into XslTransform. In this example, we also create a FileStream object to write the new HTML document to disk. If this were an ASP.NET application, we would have used a TextWriter object and passed it to the HttpResponse object instead. If we were transforming to another XML document we would have used an XmlWriter-based object. After the XPathDocument and XslTransform objects are ready, we create the XPathNavigator on the XPathDocument, and pass the XPathNavigator and the FileStream into the Transform() method of the XslTransform object. Transform() has several overloads, passing in combinations of navigators, XsltArgumentList (more on this shortly), IO streams and XmlResolvers. The navigator parameter can be XPathNavigator, or anything that implements the IXPathNavigable interface. The IO streams can be a TextWriter, Stream, or XmlWriter-based object. The XmlResolver is used to manage the process of getting a resource from an external source. The XmlResolver handles the security, opening the data source and returning the data or stream. In .Net Framework 1.0 the XmlResolver parameter was not a requirement. All of those versions of the Transform method have been deprecated and now the XmlResolver parameter is required, however you can pass null if the features of an XmlResolver, namely security and credential management are not needed. The books.xsl document is a fairly straightforward style sheet. The document looks like this: <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”/”> <html> <head> <title>Price List</title> </head> <body> <table> <xsl:apply-templates/> </table> </body> </html> </xsl:template> <xsl:template match=”bookstore”> <xsl:apply-templates select=”book”/> </xsl:template> <xsl:template match=”book”> <tr><td> <xsl:value-of select=”title”/> 808 Chapter 23 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 808 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com </td><td> <xsl:value-of select=”price”/> </td></tr> </xsl:template> </xsl:stylesheet> Using XsltArgumentList Earlier we mentioned XsltArgumentList. This is a way that you can bind an object with methods to a namespace. Once this is done, you can invoke the methods during the transform. Let’s look at an exam- ple to see how this works (located in XPathXSLSample4). Add the highlighted code to your sample code: private void button1_Click(object sender, System.EventArgs e) { //new XPathDocument XPathDocument doc=new XPathDocument(“ \\ \\ \\booksxpath.xml”); //new XslTransform XslTransform transForm=new XslTransform(); transForm.Load(“ \\ \\ \\booksarg.xsl”); //new XmlTextWriter since we are creating a new XML document XmlWriter xw=new XmlTextWriter(“ \\ \\ \\argSample.xml”,null); //create the XsltArgumentList and new BookUtils object XsltArgumentList argBook=new XsltArgumentList(); BookUtils bu=new BookUtils(); //this tells the argumentlist about BookUtils argBook.AddExtensionObject(“urn:ProCSharp”,bu); //new XPathNavigator XPathNavigator nav=doc.CreateNavigator(); //do the transform transForm.Transform(nav,argBook,xw,null); xw.Close(); } //simple test class public class BookUtils { public BookUtils(){} public string ShowText() { return “This came from the ShowText method!”; } } This is what the output of the transform looks like; we’ve formatted the output for easier viewing (argSample.xml): <books> <discbook> <booktitle>The Autobiography of Benjamin Franklin</booktitle> <showtext>This came from the ShowText method!</showtext> </discbook> <discbook> <booktitle>The Confidence Man</booktitle> 809 Manipulating XML 28 557599 Ch23.qxd 4/29/04 11:38 AM Page 809 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... 18 39 0 10 false 100 This is the new product 12 2 12... serialized The only differences between this and any other class that you may write are the C# attributes that have been added The XmlRootAttribute and XmlElementAttribute classes in the attributes inherit from the System.Attribute class Don’t confuse these attributes with the attributes in an XML document A C# attribute is simply some declarative information that can be retrieved at runtime by the... WriteXml() This method has four overloads One takes a string, which is the path and file name of where to write the XML document The second overload uses an object that is based on the XmlWriter class The third overload uses an object that is based on the TextWriter class The fourth overload is derived from the Stream class Also, if we wanted to persist the XML document to disk, we would have used something... in the XMLProducts DataSet: Products and Suppliers The relation is that Suppliers supply Products We create a new relation on the column SupplierId in both tables This is what the DataSet looks like: 8 17 Chapter 23 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 23-10 By making the same WriteXml() method call that we did in the previous example, we will get the following... Charlotte Cooper Purchasing Manager 49 Gilbert St. London EC1 4SD UK ( 171 ) 555-2222 The schema includes both DataTables that were in the DataSet In addition, the data includes all of the data from both tables For the sake of brevity, we... XsltArgumentList Then when we want to make the method call, we use standard XSLT namespace prefixing syntax and make the method call Another way we could have accomplished this is with XSLT scripting You can include C#, Visual Basic, and JavaScript code in the style sheet The great thing about this is that unlike current non-.NET implementations the script is compiled at the XslTransform.Load() call; this way you... classes are: ❑ Determine if the data should be an attribute or element ❑ Specify the namespace ❑ Change the attribute or element name The links between your object and the XML document are the custom C# attributes that annotate your classes These attributes are what are used to inform the serializer how to write out the data There is a tool, xsd.exe, that is included with.NET Framework that can help... XmlSerialization ❑ Generate an XSD file from classes that you have already developed ❑ Limit which elements are created in code ❑ Determine which programming language the generated code should be in (C#, Visual Basic NET, or JScript NET) ❑ Create schemas from types in compiled assemblies You should refer to the Framework documentation for details of command-line options for xsd.exe Despite these capabilities,... changes in booksscript.xsl: string ShowText() { return “This came from the ShowText method!”; } ... [System.Xml.Serialization.XmlElementAttribute(IsNullable=false)] public string ProductName; [System.Xml.Serialization.XmlElementAttribute()] public int SupplierID; [System.Xml.Serialization.XmlElementAttribute()] 8 27 Chapter 23 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com public int CategoryID; [System.Xml.Serialization.XmlElementAttribute()] public string QuantityPerUnit; [System.Xml.Serialization.XmlElementAttribute()] . transForm = new XslTransform(); transForm.Load(“ \ \ \books.xsl”); 8 07 Manipulating XML 28 5 575 99 Ch23.qxd 4/29/04 11:38 AM Page 8 07 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com //this. column SupplierId in both tables. This is what the DataSet looks like: 8 17 Manipulating XML 28 5 575 99 Ch23.qxd 4/29/04 11:38 AM Page 8 17 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure. ele- ment names and element values. Figure 23 -7 shows what the screen looks like after running the code. Note that novels are the only books listed now. Figure 23 -7 What if we wanted to add up the cost

Ngày đăng: 13/08/2014, 15:21

Mục lục

  • The Common Language Runtime

    • Advantages of Managed Code

    • A Closer Look at Intermediate Language

      • Support for Object Orientation and Interfaces

      • Distinct Value and Reference Types

      • Error Handling with Exceptions

      • Chapter 2: C# Basics

        • Before We Start

        • Our First C# Program

          • The Code

          • Compiling and Running the Program

          • Predefined Data Types

            • Value Types and Reference Types

            • The Main() Method

              • Multiple Main() Methods

              • Passing Arguments to Main()

              • More on Compiling C# Files

              • Using Comments

                • Internal Comments Within the Source Files

                • The C# Preprocessor Directives

                  • #define and #undef

                  • #if, #elif, #else, and #endif

                  • C# Programming Guidelines

                    • Rules for Identifiers

                    • Chapter 3: Objects and Types

                      • Classes and Structs

                      • Structs

                        • Structs Are Value Types

                        • Chapter 4: Inheritance

                          • Types of Inheritance

                            • Implementation Versus Interface Inheritance

                            • Calling Base Versions of Functions

                            • Abstract Classes and Functions

Tài liệu cùng người dùng

Tài liệu liên quan