working with xml in the.net framework. accessing an xml file basic activities: open it, read it.net...
TRANSCRIPT
WORKING WITH XML IN WORKING WITH XML IN THE .NET FRAMEWORKTHE .NET FRAMEWORK
Accessing an XML FileAccessing an XML File
• Basic activities: open it, read it• .NET Framework provides structured
and unstructured mechanisms for processing XML files
• i.e.: stream of data, or hierarchical structure
• Basic activities: open it, read it• .NET Framework provides structured
and unstructured mechanisms for processing XML files
• i.e.: stream of data, or hierarchical structure
XML Classes We Will UseXML Classes We Will Use
• XmlReader - process as you go• XmlNode – represents nodes in an
XML file• XmlDocument – represents entire
XML document• XmlDataDocument – data enabled
• XmlReader - process as you go• XmlNode – represents nodes in an
XML file• XmlDocument – represents entire
XML document• XmlDataDocument – data enabled
• <?xml version="1.0" encoding="utf-8" ?>• <Books>• <Book Pages ="1008">• <Author>Delaney, Kalen</Author>• <Title>Inside Microsoft SQL Server</Title>• <Publisher>Microsoft Press</Publisher>• </Book>• <Book Pages ="997">• <Author>Burton, Kevin</Author>• <Title>.NET Common Language Runtime</Title>• <Publisher>Sams</Publisher>• </Book>• <Book Pages ="392">• <Author>Cooper, James W.</Author>• <Title>C# Design Patterns</Title>• <Publisher>Addison Wesley</Publisher>• </Book>• <Book Pages ="2">• <Author>Hull, Dale E.</Author>• <Title>The Joys of SOA and Web Services With .NET</Title>• <Publisher>Fulfilled Life Press</Publisher>• </Book>• <Book Pages ="214">• <Author>Guy, Class E.</Author>• <Title>Dressing For Success As A .NET Developer</Title>• <Publisher>Gentleman's Quarterly</Publisher>• </Book>• </Books>
• <?xml version="1.0" encoding="utf-8" ?>• <Books>• <Book Pages ="1008">• <Author>Delaney, Kalen</Author>• <Title>Inside Microsoft SQL Server</Title>• <Publisher>Microsoft Press</Publisher>• </Book>• <Book Pages ="997">• <Author>Burton, Kevin</Author>• <Title>.NET Common Language Runtime</Title>• <Publisher>Sams</Publisher>• </Book>• <Book Pages ="392">• <Author>Cooper, James W.</Author>• <Title>C# Design Patterns</Title>• <Publisher>Addison Wesley</Publisher>• </Book>• <Book Pages ="2">• <Author>Hull, Dale E.</Author>• <Title>The Joys of SOA and Web Services With .NET</Title>• <Publisher>Fulfilled Life Press</Publisher>• </Book>• <Book Pages ="214">• <Author>Guy, Class E.</Author>• <Title>Dressing For Success As A .NET Developer</Title>• <Publisher>Gentleman's Quarterly</Publisher>• </Book>• </Books>
Document Object Model (DOM)Document Object Model (DOM)
• Internet standard for representing the information contained in an HTML or XML file as a tree of nodes
• .NET Framework supports Level 1 core specs and Level 2 core specs
• It extends the specifications by adding objects, methods and properties not defined in the spec
• Internet standard for representing the information contained in an HTML or XML file as a tree of nodes
• .NET Framework supports Level 1 core specs and Level 2 core specs
• It extends the specifications by adding objects, methods and properties not defined in the spec
XML Document StructureXML Document Structure
• Series of nested items, including elements and attributes
• Any nested structure can be transformed into an equivalent tree structure if the outermost nested item is made the root of the tree
• The next nested items are children of the root, etc.
• Series of nested items, including elements and attributes
• Any nested structure can be transformed into an equivalent tree structure if the outermost nested item is made the root of the tree
• The next nested items are children of the root, etc.
The XmlReader ClassThe XmlReader Class
• Forward-only, read-only access to an XML file
• Similar to a cursor in processing a relational database
• Move through the file with Read()• Read() returns the next node
• Forward-only, read-only access to an XML file
• Similar to a cursor in processing a relational database
• Move through the file with Read()• Read() returns the next node
Important Properties of XmlReaderImportant Properties of XmlReader
• Depth• EOF
HasAttributes• IsEmptyElement• Name• NodeType• Value
• Depth• EOF
HasAttributes• IsEmptyElement• Name• NodeType• Value
Important Methods of XmlReaderImportant Methods of XmlReader
• GetAttribute()IsStartElement()
• MoveToElement()• MoveToFirstElement()• MoveToNextAttribute()• Read()• Skip()
• GetAttribute()IsStartElement()
• MoveToElement()• MoveToFirstElement()• MoveToNextAttribute()• Read()• Skip()
XmlTextReaderXmlTextReader
• XmlReader is an abstract class
• You will use XmlTextReader, which inherits from XmlReader, and implements its methods
• Used with text streams
• XmlReader is an abstract class
• You will use XmlTextReader, which inherits from XmlReader, and implements its methods
• Used with text streams
Demonstration ProjectsDemonstration Projects
• XmlTextReader – uses XmlTextReader to retrieve all entries
• AttributesElements – using XmlTextReader to retrieve selected entities
• XmlDocument - uses XmlDocument and XmlNode classes to retrieve node objects from a Document object
• XmlTextReader – uses XmlTextReader to retrieve all entries
• AttributesElements – using XmlTextReader to retrieve selected entities
• XmlDocument - uses XmlDocument and XmlNode classes to retrieve node objects from a Document object
Demonstration Projects (cont’d)Demonstration Projects (cont’d)
• DataSetFromXml – synchronizes by retrieving a DataSet object from an XmlDataDocument object
• XmlFromDataSet – creates an XmlDataDocument from a DataSet object
• SynchronizeWithXmlSchema – uses an XML schema to create a DataSet object
• DataSetFromXml – synchronizes by retrieving a DataSet object from an XmlDataDocument object
• XmlFromDataSet – creates an XmlDataDocument from a DataSet object
• SynchronizeWithXmlSchema – uses an XML schema to create a DataSet object
The XmlNode ClassThe XmlNode Class
• Individual items in a tree representation of an XML file are called “nodes”
• The DOM assigns node types to different kinds of nodes
• In the .NET Framework, the possible node types are listed in the XmlNodeType enumeration
• Individual items in a tree representation of an XML file are called “nodes”
• The DOM assigns node types to different kinds of nodes
• In the .NET Framework, the possible node types are listed in the XmlNodeType enumeration
Members of The XmlNodeType Enumeration
Members of The XmlNodeType Enumeration
• Attribute• Comment• Document• DocumentFragment• Element• EndElement• EndEntity
• Attribute• Comment• Document• DocumentFragment• Element• EndElement• EndEntity
Members of The XmlNodeType Enumeration (cont’d)
Members of The XmlNodeType Enumeration (cont’d)
• Entity• EntityReference• None• Notation• ProcessingInstruction• SignificantWhitespace• Text• Whitespace• XmlDeclaration
• Entity• EntityReference• None• Notation• ProcessingInstruction• SignificantWhitespace• Text• Whitespace• XmlDeclaration
The XmlNodeClassThe XmlNodeClass
• Has a rich set of properties and methods• You can retrieve or set information about an
entity represented by an XmlNode object• You can use its methods to navigate the
DOM
• Has a rich set of properties and methods• You can retrieve or set information about an
entity represented by an XmlNode object• You can use its methods to navigate the
DOM
Important Properties of The XmlNode Class
Important Properties of The XmlNode Class
• Attributes• ChildNodes• FirstChild• HasChildNodes• InnerText• InnerXml• LastChild
• Attributes• ChildNodes• FirstChild• HasChildNodes• InnerText• InnerXml• LastChild
Important Properties of The XmlNode Class (cont’d)
Important Properties of The XmlNode Class (cont’d)
• Name• NextSibling• NodeType• OuterXml• OwnerDocument• ParentNode• PreviousSibling
• Name• NextSibling• NodeType• OuterXml• OwnerDocument• ParentNode• PreviousSibling
Important Methods of The XmlNode Class
Important Methods of The XmlNode Class
• AppendChild()• CloneNode()• InsertAfter()• InsertBefore()• PrependChild()• RemoveAll()
• AppendChild()• CloneNode()• InsertAfter()• InsertBefore()• PrependChild()• RemoveAll()
Important Methods of The XmlNode Class (cont’d)
Important Methods of The XmlNode Class (cont’d)
• RemoveChild()• ReplaceChild()• SelectNodes()• SelectSingleNode()• WriteContentTo()• WriteTo()
• RemoveChild()• ReplaceChild()• SelectNodes()• SelectSingleNode()• WriteContentTo()• WriteTo()
Synchronizing DataSet Objects With XML
Synchronizing DataSet Objects With XML
• The DataSet object is an ADO.NET object that represents the structure and data of a relational database
• The System.Xml namespace automatically synchronizes a DataSet object with an XML file
• The DataSet object is an ADO.NET object that represents the structure and data of a relational database
• The System.Xml namespace automatically synchronizes a DataSet object with an XML file
The XmlDocument ClassThe XmlDocument Class
• Represents an entire XML document• XmlNode objects are associated with
an XmlDocument object• You navigate through the DOM
representation of an XML document with these two classes
• Represents an entire XML document• XmlNode objects are associated with
an XmlDocument object• You navigate through the DOM
representation of an XML document with these two classes
Additional Members of The XmlDataDocument Class
Additional Members of The XmlDataDocument Class
• DataSet
• GetElementFromRow()
• GetRowFromElement()
• Load()
• DataSet
• GetElementFromRow()
• GetRowFromElement()
• Load()
Synchronization ProcessSynchronization Process
• You can start with– An XmlDataDocument object– A full DataSet object– A schema-only DataSet object
• You can start with– An XmlDataDocument object– A full DataSet object– A schema-only DataSet object