Home | | Service Oriented Architecture | The System.Xml Namespace

Chapter: XML and Web Services : Building XML-Based Applications : Leveraging XML in Visual Studio .NET

The System.Xml Namespace

XML within the .NET framework is a key component, and as such, Microsoft has pro-vided a comprehensive set of classes to perform all sorts of tasks related to the opera-tions dealing with XML.

The System.Xml Namespace

 

XML within the .NET framework is a key component, and as such, Microsoft has pro-vided a comprehensive set of classes to perform all sorts of tasks related to the opera-tions dealing with XML. Everything from reading and writing XML, to parsing XML, to validating XML, to performing transformations on XML, and more can be found within the classes provided by the .NET framework.

 

The System.Xml namespace within the Visual Studio .NET Framework provides the set of classes necessary to perform everything from simple to complex operations on XML data. The following classes may be found within the System.Xml namespace:

 

   XmlDocument

 

   XmlNode

 

   XmlNodeList

 

   XmlNamedNodeMap

 

   XmlDataDocument

 

   XmlWriter

 

   XmlTextWriter

 

   XmlReader

 

   XmlTextReader

 

In addition to these classes, the System.Xml namespace contains a set of child name-spaces that contain other classes to perform more specialized operations:

 

   System.Xml.Schema

 

   System.Xml.Serialization

 

   System.Xml.XPath

 

   System.Xml.Xsl

 

These namespaces, as you can probably guess by their names, provide classes to deal with specific operations or provide specific functionality related to schemas, serializa-tion, XPath expressions, and XSLT transformations.

The XmlDocument Class

 

The XmlDocument class found in the System.Xml namespace is a .NET implementation of the core W3C Document Object Model (DOM), levels 1 and 2. Because this particular class is nothing more than another DOM for XML, we’ll forego spending a lot of time going over how to use it, because Chapter 7, “Parsing XML Using Document Object Model,” has already done a good job of teaching you how to use a DOM object. Instead, what we’ll do here is list the various members of the XmlDocument class, such as the pub-lic instance properties of the XmlDocument class, as shown in Table 17.9.


 

TABLE 17.9    Some of the Public Instance Properties of the XmlDocument Class

Name : Description

 

Attributes : Holds a reference to an XmlAttributeCollection object

BaseURI : Indicates the base URI of the current node

ChildNodes : Holds a reference to an XmlNodeList object

DocumentElement : The root XmlElement for the document

DocumentType : Holds a reference to the DOCTYPE node within the document

FirstChild : Holds a reference to the first child node of the current node

HasChildNodes : Indicates whether the current node has any child nodes

Implementation : Holds a reference to the XmlImplementation object for the current document

InnerText : Specifies the concatenated values of the current node and all its children

InnerXml : Specifies the XML fragment of the children of the current node

IsReadOnly : Indicates whether the current node is read-only

Item : Returns the specified child element

LastChild : Holds a reference to the last child of the current node

LocalName : Indicates the local name of the current node

Name : Indicates the qualified name of the current node

NamespaceURI : Indicates the namespace URI for the current node

NameTable : Holds a reference to the XmlNameTable object associated with this implementation

NextSibling : Holds a reference to the next node following the current node

NodeType : Indicates the type of node

OuterXml : Returns the XML fragment of the current node and its child nodes

OwnerDocument : Holds a reference to the XmlDocument object for the current node

ParentNode : Holds a reference to the parent node of the current node

Prefix : Specifies the namespace prefix for the current node

PreserveWhitespace : Specifies whether whitespace should be preserved

PreviousSibling : Holds a reference to the node immediately preceding the current node

Value : Specifies the value of the current node

XmlResolver : Sets the XmlResolver object to use for resolving external resources

 

 

Chapter 7 covered the XML Document Object Model (DOM) in detail. Microsoft, ever striving to merge a defined “standard” and its vision of a “standard,” hasn’t forgotten how important the DOM is to developers. However, rather than simply use the current COM implementation of the DOM, Microsoft has decided to include a specialized class in the .NET Framework that is a DOM implementation that will run in the managed environment of the CLR. Table 17.10 lists the public instance methods available for the

 

XmlDocument class.

 

TABLE 17.10  Some of the Public Instance Methods of the XmlDocument Class

 

Name : Description

 

AppendChild : Adds the specified node to the end of the child node of the current node

Clone : Creates a duplicate of the current node

CloneNode : Creates a duplicate of the current node

CreateAttribute : Creates an XmlAttribute object with the specified name

CreateCDataSection : Creates an XmlCDataSection object containing the specified data

CreateComment : Creates an XmlComment object containing the specified data

CreateDocumentFragment : Creates an XmlDocumentFragment object

CreateDocumentType : Creates an XmlDocumentType object

CreateElement : Creates an XmlElement object

CreateEntityReference : Creates an XmlEntityReference object with the  : specified name

CreateNavigator : Creates an XPathNavigator object for navigating this object

CreateNode : Creates an XmlNode object

CreateProcessingInstruction : Creates an XmlProcessingInstruction object with the specified name and data

CreateSignificantWhitespace : Creates an XmlSignificantWhitespace object

CreateTextNode : Creates an XmlText object with the specified text

CreateWhitespace : Creates an XmlWhitespace object

CreateXmlDeclaration : Creates an XmlDeclaration object with the specified values

Equals : Determines whether two object instances are equal

GetElementById : Gets the XmlElement object with the specified ID

GetElementsByTagName : Returns an XmlNodeList object containing a list of all descendant elements that match the specified name

GetEnumerator : Provides support for the for-each style iteration over the nodes in the specified node

GetNamespaceOfPrefix : Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the name-space URI in the declaration

GetPrefixOfNamespace : Looks up the closest xmlns declaration for the given name- space URI that is in scope for the current node and returns the prefix defined in that declaration

GetType : Gets the type of the current instance

ImportNode : Imports a node from another document into the current document

InsertAfter : Inserts a node immediately after the specified node

InsertBefore : Inserts a node immediately before the specified node

Load : Loads the specified XML data

LoadXml : Loads the XML document from the specified string

CreateElement : Creates an XmlElement object

CreateEntityReference : Creates an XmlEntityReference object with the specified name

CreateNavigator : Creates an XPathNavigator object for navigating this object

CreateNode : Creates an XmlNode object

CreateProcessingInstruction : Creates an XmlProcessingInstruction object with the specified name and data

CreateSignificantWhitespace : Creates an XmlSignificantWhitespace object

CreateTextNode : Creates an XmlText object with the specified text

CreateWhitespace : Creates an XmlWhitespace object

CreateXmlDeclaration : Creates an XmlDeclaration object with the specified values

Equals : Determines whether two object instances are equal

GetElementById : Gets the XmlElement object with the specified ID

GetElementsByTagName : Returns an XmlNodeList object containing a list of all descendant elements that match the specified name

GetEnumerator : Provides support for the for-each style iteration over the nodes in the specified node

GetNamespaceOfPrefix : Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the name-space URI in the declaration

GetPrefixOfNamespace : Looks up the closest xmlns declaration for the given name- space URI that is in scope for the current node and returns the prefix defined in that declaration

GetType : Gets the type of the current instance

ImportNode : Imports a node from another document into the current document

InsertAfter : Inserts a node immediately after the specified node

InsertBefore : Inserts a node immediately before the specified node

Load : Loads the specified XML data

LoadXml : Loads the XML document from the specified string

Normalize : Puts all XmlText nodes in the full depth of the subtree underneath the current node into a “normal” form, where only markup separates XmlText nodes (that is, there are no adjacent XmlText nodes)

PrependChild : Adds the specified node to the beginning of the child nodes for the current node

ReadNode : Creates an XmlNode object based on the information in an XmlReader object

RemoveAll : Removes all the children and/or attributes of the current node

RemoveChild : Removes the specified child node

ReplaceChild : Replaces the specified child node with another node

Save : Saves the XML document to the specified location

SelectNodes : Selects a list of nodes matching the XPath expression

SelectSingleNode : Selects the first node that matches the XPath expression

Supports : Tests whether the DOM implementation implements a specific feature

ToString : Returns a string that represents the current object

WriteContentTo : Saves all the children of the XmlDocument node to the specified XmlWriter object

WriteTo : Saves the XmlDocument node to the specified XmlWriter object

 

The XmlDataDocument Class

 

You’ve already seen how you can load hierarchical XML data into a data set so that you can access the information in a relational manner. You’ve also seen how you can create hierarchical XML data from a data set that can then be loaded into an XmlDocument object from which you can perform operations on that XML. However, the steps neces-sary to perform these tasks are less than ideal. Wouldn’t it be great if you could simply load the information contained within a data set directly into a DOM object from which you could perform data manipulations? Believe it or not, within Visual Studio .NET, you can.

The Visual Studio .NET Framework provides a class that allows you to do just that. The XML framework and the ADO.NET Framework provide a unified programming model

to access XML as well as relational data. The XmlDataDocument class within the XML framework provides the necessary bridge between ADO.NET and the XML framework. Table 17.11 lists some of the public instance properties of the XmlDataDocument class.


TABLE 17.11  Some of the Public Instance Properties Available on the XmlDataDocument Class


Name : Description

 

Attributes : Holds a reference to an XmlAttributeCollection object

BaseURI : Indicates the base URI of the current node

ChildNodes : Holds a reference to an XmlNodeList object

DataSet : Holds a reference to the data set that provides the relational  : data for the XmlDataDocument object

DocumentElement : The root XmlElement for the document

DocumentType : Holds a reference to the DOCTYPE node within the document

FirstChild : Holds a reference to the first child node of the current node

HasChildNodes : Indicates whether the current node has any child nodes

Implementation : Holds a reference to the XmlImplementation object for the current document

InnerText : Specifies the concatenated values of the current node and all its children

InnerXml : Specifies the XML fragment of the children of the current node

IsReadOnly : Indicates whether the current node is read-only

Item : Returns the specified child element

LastChild : Holds a reference to the last child of the current node

LocalName : Indicates the local name of the current node

Name : Indicates the qualified name of the current node

NamespaceURI : Indicates the namespace URI for the current node

NameTable : Holds a reference to the XmlNameTable object associated with this implementation

NextSibling : Holds a reference to the next node following the current node

NodeType : Indicates the type of node

OuterXml : Returns the XML fragment of the current node and its child nodes

OwnerDocument : Holds a reference to the XmlDataDocument object for the current node

ParentNode : Holds a reference to the parent node of the current node

Prefix : Specifies the namespace prefix for the current node

PreserveWhitespace : Specifies whether whitespace should be preserved

PreviousSibling : Holds a reference to the node immediately preceding the current node

Value : Specifies the value of the current node

XmlResolver : Sets the XmlResolver object to use for resolving external resources

 

 

Because XmlDataDocument is a subclass of the XmlDocument class, it inherits all the prop-erties, events, and methods from the XmlDocument class. However, the XmlDataDocument class provides some additional methods that help manage relational data as hierarchical data. Table 17.12 lists some of the public instance methods available for the XmlDataDocument class.

 

TABLE 17.12 Some of the Public Instance Methods Available of the XmlDataDocument Class


Name : Description

 

AppendChild : Adds the specified node to the end of the child node of the current node.

Clone : Creates a duplicate of the current node.

CloneNode : Creates a duplicate of the current node.

CreateAttribute : Creates an XmlAttribute object with the specified name.

CreateCDataSection : Creates an XmlCDataSection object containing the specified data.

CreateComment : Creates an XmlComment object containing the specified data.

CreateDocumentFragment : Creates an XmlDocumentFragment object.

CreateDocumentType : Creates an XmlDocumentType object.

CreateElement : Creates an XmlElement object.

CreateEntityReference : This method is not supported by the XmlDataDocument class. If it’s called, an exception will be thrown.

CreateNavigator : Creates an XPathNavigator object for navigating this object.

CreateNode : Creates an XmlNode object.

CreateProcessingInstruction : Creates an XmlProcessingInstruction object with the specified name and data.

CreateSignificantWhitespace : Creates an XmlSignificantWhitespace object.

CreateTextNode : Creates an XmlText object with the specified text.

CreateWhitespace : Creates an XmlWhitespace object.

CreateXmlDeclaration : Creates an XmlDeclaration object with the specified values.

Equals : Determines whether two object instances are equal.

GetElementById : This method is not supported by the XmlDataDocument class. If it’s called, an exception will be thrown.

GetElementFromRow : Returns the XmlElement object associated with the specified DataRow object.

GetElementsByTagName : Returns an XmlNodeList object containing a list of all the descendant elements that match the specified name.

GetEnumerator : Provides support for the for-each style iteration over the nodes in the specified node.

GetNamespaceOfPrefix : Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the name-space URI in the declaration.

GetPrefixOfNamespace : Looks up the closest xmlns declaration for the given name-space URI that is in scope for the current node and returns the prefix defined in that declaration.

GetRowFromElement : Returns the DataRow object for the specified XmlElement object.

GetType : Gets the type of the current instance.

ImportNode : Imports a node from another document into the current document.

InsertAfter : Inserts a node immediately after the specified node.

InsertBefore : Inserts a node immediately before the specified node.

Load : Loads the specified XML data and synchronizes the XmlDataDocument object with the data set.

LoadXml : Loads the XML document from the specified string.

Normalize : Puts all XmlText nodes in the full depth of the subtree  underneath the current node into a “normal” form, where only markup separates XmlText nodes (in other words, there are no adjacent XmlText nodes).

PrependChild : Adds the specified node to the beginning of the child nodes for the current node.

ReadNode : Creates an XmlNode object based on the information in an XmlReader object.

RemoveAll : Removes all the children and/or attributes of the current node.

RemoveChild : Removes the specified child node.

ReplaceChild : Replaces the specified child node with another node.

Save : Saves the XML document to the specified location.

SelectNodes : Selects a list of nodes matching the XPath expression.

SelectSingleNode : Selects the first node that matches the XPath expression.

Supports : Tests whether the DOM implementation implements a specific feature.

ToString : Returns a string that represents the current object.

WriteContentTo : Saves all the children of the XmlDocument node to the speci-fied XmlWriter object.

WriteTo : Saves the XmlDocument node to the specified XmlWriter object.

 

 

You can see from Tables 17.11 and 17.12 that in addition to the properties and methods found in the XmlDocument class, the XmlDataDocument class has a few specialized prop-erties and methods that integrate the XmlDocument class with the DataSet class.

The following C# code demonstrates how to create an XmlDataDocument object from a given data set:

 

string  cConn  =  “Data  Source=TRAVISNOTEBOOK\\SQL2000;Integrated

 

Security=SSPI;Initial Catalog=Northwind”; System.Data.SqlClient.SqlConnection oConn = new System.Data.SqlClient.SqlConnection(cConn);

oConn.Open();

 

System.Data.SqlClient.SqlDataAdapter  oDA  =  new

System.Data.SqlClient.SqlDataAdapter(

 

”Select * From Customers Where CustomerID Like ’A%’”, oConn);

 

System.Data.DataSet  oDS  =  new  System.Data.DataSet();

 

oDA.Fill(oDS,”Customers”);

 

oConn.Close();

 

System.Xml.XmlDataDoument  oXML  =

new  System.Xml.XmlDataDocument(oDS);

 

Here’s the code for VB .NET:

 

Dim  cConn  As  string  =  “Data  Source=

TRAVISNOTEBOOK\SQL2000;Integrated

Security=SSPI;Initial  Catalog=Northwind”

 

Dim oConn As System.Data.SqlClient.SqlConnection = new

System.Data.SqlClient.SqlConnection(cConn)

 

oConn.Open();

 

Dim  oDA  As  System.Data.SqlClient.SqlDataAdapter  =  new

System.Data.SqlClient.SqlDataAdapter(

 

”Select * From Customers Where CustomerID Like ’A%’”, oConn)

 

Dim  oDS  As  System.Data.DataSet  =  new  System.Data.DataSet()

 

oDA.Fill(oDS,”Customers”)

 

oConn.Close()

 

Dim oXML As System.Xml.XmlDataDocument = new System.Xml.XmlDataDocument(oDS)

 

Basically, the preceding code loads the relational data into a DataSet object, and then we use that DataSet object as a parameter that is passed to the constructor of the XmlDataDocument object. At this point, Visual Studio .NET does something intriguing: You may modify and manipulate the data in either object—the DataSet object or the XmlDataDocument object—and the changes will be reflected in the other object. This means you still have one data source you may modify, but you have two different objects that allow you to do so, and each object picks up on the changes made in the other object.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
XML and Web Services : Building XML-Based Applications : Leveraging XML in Visual Studio .NET : The System.Xml Namespace |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.