WORKING WITH XML IN THE .NET FRAMEWORK

basiliskcanoeΛογισμικό & κατασκευή λογ/κού

2 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

69 εμφανίσεις

WORKING WITH XML IN
THE .NET FRAMEWORK

Accessing 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

XML Classes We Will Use


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>


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

XML 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.


The 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

Important Properties of
XmlReader


Depth


EOF

HasAttributes


IsEmptyElement


Name


NodeType


Value

Important Methods of XmlReader


GetAttribute()

IsStartElement()


MoveToElement()


MoveToFirstElement()


MoveToNextAttribute()


Read()


Skip()

XmlTextReader


XmlReader is an abstract class


You will use XmlTextReader, which
inherits from XmlReader, and
implements its methods


Used with text streams

Demonstration 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

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

The 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

Members of The XmlNodeType
Enumeration


Attribute


Comment


Document


DocumentFragment


Element


EndElement


EndEntity


Members of The XmlNodeType
Enumeration (cont’d)


Entity


EntityReference


None


Notation


ProcessingInstruction


SignificantWhitespace


Text


Whitespace


XmlDeclaration

The 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

Important Properties of The
XmlNode Class


Attributes


ChildNodes


FirstChild


HasChildNodes


InnerText


InnerXml


LastChild

Important Properties of The
XmlNode Class (cont’d)


Name


NextSibling


NodeType


OuterXml


OwnerDocument


ParentNode


PreviousSibling

Important Methods of The
XmlNode Class


AppendChild()


CloneNode()


InsertAfter()


InsertBefore()


PrependChild()


RemoveAll()

Important Methods of The
XmlNode Class (cont’d)


RemoveChild()


ReplaceChild()


SelectNodes()


SelectSingleNode()


WriteContentTo()


WriteTo()

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

Additional Members of The
XmlDataDocument Class


DataSet


GetElementFromRow()


GetRowFromElement()


Load()

Synchronization Process


You can start with


An XmlDataDocument object


A full DataSet object


A schema
-
only DataSet object