Danh mục

Visual C# 2010 Recipes solution_2

Số trang: 95      Loại file: pdf      Dung lượng: 2.05 MB      Lượt xem: 12      Lượt tải: 0    
Jamona

Phí tải xuống: 37,000 VND Tải xuống file đầy đủ (95 trang) 0
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Khi bạn đang phải đối mặt với một Visual C # 2010 vấn đề, cuốn sách này có thể chứa một công thức cung cấp giải pháp hoặc ít nhất là điểm bạn đi đúng hướng. Ngay cả khi bạn chỉ đơn giản là tìm cách để mở rộng kiến thức của bạn của thư viện NET. Framework, Visual C # 2010 Recipes là tài nguyên hoàn hảo để giúp bạn .
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_2CHAPTER 6■■■XML ProcessingOne of the most remarkable aspects of the Microsoft .NET Framework is its deep integration with XML.In many .NET applications, you won’t even be aware you’re using XML technologies—they’ll just beused behind the scenes when you serialize a Microsoft ADO.NET DataSet, call a web service, or readapplication settings from a Web.config configuration file. In other cases, you’ll want to work directly withthe System.Xml namespaces to manipulate Extensible Markup Language (XML) data. Common XMLtasks don’t just include parsing an XML file, but also include validating it against a schema, applying anExtensible Stylesheet Language (XSL) transform to create a new document or Hypertext MarkupLanguage (HTML) page, and searching intelligently with XPath. In .NET 3.5, Microsoft added LINQ to XML, which integrates XML handling into the LINQ model forquerying data sources. You can use the same keywords and syntax to query XML as you would acollection or a database. The recipes in this chapter describe how to do the following: • Read, parse, and manipulate XML data (recipes 6-1, 6-2, 6-3, and 6-7) • Search an XML document for specific nodes, either by name (recipe 6-4), by namespace (recipe 6-5), or by using XPath (recipe 6-6) • Validate an XML document with an XML schema (recipe 6-8) • Serialize an object to XML (recipe 6-9), create an XML schema for a class (recipe 6- 10), and generate the source code for a class based on an XML schema (recipe 6- 11) • Transform an XML document to another document using an XSL Transformations (XSLT) stylesheet (recipe 6-12) • Use LINQ to XML to load, create, query and modify XML trees (recipes 6-13, 6-14, 6-15, and 6-16).6-1. Show the Structure of an XML Document in a TreeViewProblemYou need to display the structure and content of an XML document in a Windows-based application. 261 CHAPTER 6 ■ XML PROCESSING Solution Load the XML document using the System.Xml.XmlDocument class. Create a reentrant method that converts a single XmlNode into a System.Windows.Forms.TreeNode, and call it recursively to walk through the entire document. How It Works The .NET Framework provides several different ways to process XML documents. The one you use depends in part upon your programming task. One of the most fully featured classes is XmlDocument, which provides an in-memory representation of an XML document that conforms to the W3C Document Object Model (DOM). The XmlDocument class allows you to browse through the nodes in any direction, insert and remove nodes, and change the structure on the fly. For details of the DOM specification, go to www.w3c.org. ■ Note The XmlDocument class is not scalable for very large XML documents, because it holds the entire XML content in memory at once. If you want a more memory-efficient alternative, and you can afford to read and process the XML piece by piece, consider the XmlReader and XmlWriter classes described in recipe 6-7. To use the XmlDocument class, simply create a new instance of the class and call the Load method with a file name, a Stream, a TextReader, or an XmlReader object. It is also possible to read the XML from a simple string with the LoadXML method. You can even supply a string with a URL that points to an XML document on the Web using the Load method. The XmlDocument instance will be populated with the tree of elements, or nodes, from the source document. The entry point for accessing these nodes is the root element, which is provided through the XmlDocument.DocumentElement property. DocumentElement is an XmlElement object that can contain one or more nested XmlNode objects, which in turn can contain more XmlNode objects, and so on. An XmlNode is the basic ingredient of an XML file. Common XML nodes include elements, attributes, comments, and contained text. When dealing with an XmlNode or a class that derives from it (such as XmlElement or XmlAttribute), you can use the following basic properties: • ChildNodes is an XmlNodeList collection that contains the first level of nested nodes. • Name is the name of the node. • NodeType returns a member of the System.Xml.XmlNodeType enumeration that indicates the type of the node (element, attribute, text, and so on). • Value is the content of the node, if it’s a text or CDATA node. • Attributes provides a collection of node objects representing the attributes ...

Tài liệu được xem nhiều: