working with xml in the.net framework. accessing an xml file basic activities: open it, read it.net...

24
WORKING WITH XML IN WORKING WITH XML IN THE .NET FRAMEWORK THE .NET FRAMEWORK

Upload: ezra-boyd

Post on 01-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

WORKING WITH XML IN WORKING WITH XML IN THE .NET FRAMEWORKTHE .NET FRAMEWORK

Page 2: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 3: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 4: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

• <?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>

Page 5: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 6: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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.

Page 7: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 8: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

Important Properties of XmlReaderImportant Properties of XmlReader

• Depth• EOF

HasAttributes• IsEmptyElement• Name• NodeType• Value

• Depth• EOF

HasAttributes• IsEmptyElement• Name• NodeType• Value

Page 9: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

Important Methods of XmlReaderImportant Methods of XmlReader

• GetAttribute()IsStartElement()

• MoveToElement()• MoveToFirstElement()• MoveToNextAttribute()• Read()• Skip()

• GetAttribute()IsStartElement()

• MoveToElement()• MoveToFirstElement()• MoveToNextAttribute()• Read()• Skip()

Page 10: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 11: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 12: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 13: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 14: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

Members of The XmlNodeType Enumeration

Members of The XmlNodeType Enumeration

• Attribute• Comment• Document• DocumentFragment• Element• EndElement• EndEntity

• Attribute• Comment• Document• DocumentFragment• Element• EndElement• EndEntity

Page 15: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 16: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 17: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 18: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 19: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

Important Methods of The XmlNode Class

Important Methods of The XmlNode Class

• AppendChild()• CloneNode()• InsertAfter()• InsertBefore()• PrependChild()• RemoveAll()

• AppendChild()• CloneNode()• InsertAfter()• InsertBefore()• PrependChild()• RemoveAll()

Page 20: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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()

Page 21: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 22: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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

Page 23: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

Additional Members of The XmlDataDocument Class

Additional Members of The XmlDataDocument Class

• DataSet

• GetElementFromRow()

• GetRowFromElement()

• Load()

• DataSet

• GetElementFromRow()

• GetRowFromElement()

• Load()

Page 24: WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms

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