Databases Reference
In-Depth Information
<prov-state>Ontario</prov-state>
<pcode-zip>M6W 1E6</pcode-zip>
</addr>
<phone type="work">416-555-1358</phone>
</customerinfo>
The customer ID ( CID ) is also stored as an attribute in customerinfo element.
The INFO column being returned from the SELECT statement is XMLdata type,
which will be read using XmlReader object.
The XmlReader object is an event-based, read-only, forward-only XML pull
parser. It provides functionality for reading in XML documents.
XmlReader xmlreader = cmd.ExecuteXmlReader();
The XmlReader object is then loaded into XmlDocument object, which
represents XML document as a node tree, where elements and attributes are
stored as nodes that contain relational information.
XmlDocument.Load(xmlreader);
Presence of XML namespace determines whether we need to add
XmlNamespaceManager in order to successfully select nodes.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(XmlNameTable);
nsmgr.AddNamespace(prefix, uri);
If the XPath expression in SelectNodes does not include a prefix, it is assumed
that the namespace URI is the empty namespace. If XML document includes a
default namespace, it must be added to a prefix and namespace URI to the
XmlNamespaceManager or none of the nodes will get selected. The
XmlNamespaceManager is required to resolve any prefixes in the XPath
expression.
selXmlDoc.SelectNodes("//prefix:element", nsmgr);
If XML namespace is not present in XML document (in absence of xmlns
attribute), XmlNamespaceManager is not needed and XPath expression in
SelectNodes will appear as following:
selXmlDoc.SelectNodes("//element");
The double slashes (//) in XPath above refer to descendant-or-self Axis, meaning
it contains the context node in addition to all the nodes contained in the
descendant axis.
Note that the use of (//) prefix will yield all instances of element name specified in
the XML document.
Search WWH ::




Custom Search