After everything I have read and some of my own testing I am convinced
that XPathDocument are more efficient if you are only using XML for
read only and not modifying the XML. However, I have been having
problems doing same procedures I do with XPathDocuments in
XPathDocuments. I have a configuration XML document, which holds all
the configuration information for an application (see below sample).
I extract the information from the document and use it in the
application. In the XMLDocument solution (see below) I load the XML
document and build a SQL connection string. I am unable to reproduce
the same results in my XPathDocument solution attempt (se below).
Any help would be appreciated.
Thanks,
Jim
Configuration XML:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<dbconfig datasource="Jim" initialcatalog="pubs" userid="DotNet"
password="password" />
</configuration>
XMLDocument solution:
Try
Dim l_XmlDocument As New System.Xml.XmlDocument
Dim l_XmlNodeList As System.Xml.XmlNodeList
Dim l_XmlNode As System.Xml.XmlNode
Dim l_XmlAttrList As System.Xml.XmlAttributeCollection
Dim l_Buffer As New System.Text.StringBuilder
Dim l_SqlConnection As SqlClient.SqlConnection
'Open XML document and extract attributes
l_XmlDocument.Load(Concat(System.AppDomain.Current Domain.BaseDirectory().ToString,
"Configuration.xml"))
l_XmlNode =
l_XmlDocument.SelectSingleNode("/configuration/dbconfig")
l_XmlAttrList = l_XmlNode.Attributes
'Build Connection String
l_Buffer.Append("Data Source")
l_Buffer.Append("=")
l_Buffer.Append(l_XmlAttrList(0).Value.ToString)
l_Buffer.Append(";")
l_Buffer.Append("Initial Catalog")
l_Buffer.Append("=")
l_Buffer.Append(l_XmlAttrList(1).Value.ToString)
l_Buffer.Append(";")
l_Buffer.Append("User ID")
l_Buffer.Append("=")
l_Buffer.Append(l_XmlAttrList(2).Value.ToString)
l_Buffer.Append(";")
l_Buffer.Append("Password")
l_Buffer.Append("=")
l_Buffer.Append(l_XmlAttrList(3).Value.ToString())
l_Buffer.Append(";")
l_Buffer.Append("Max Pool Size")
l_Buffer.Append("=")
l_Buffer.Append("500")
l_Buffer.Append(";")
'Open Connection
l_SqlConnection = New
SqlClient.SqlConnection(l_Buffer.ToString)
l_SqlConnection.Open()
'Do SQL stuff here
l_SqlConnection.Close()
Catch ex As Exception
System.Console.WriteLine("Exception: " + ex.Message)
End Try
XPathDocument solution:
Try
Dim l_strText As String
Dim l_XPathDoc As New
System.Xml.XPath.XPathDocument(Concat( _
System.AppDomain.CurrentDomain.BaseDirectory().ToS tring,
_
"Configuration\Localization.xml"))
Dim l_XPathNavigator As System.Xml.XPath.XPathNavigator =
l_XPathDoc.CreateNavigator()
Dim l_Iterator As System.Xml.XPath.XPathNodeIterator =
l_XPathNavigator.Select("/configuration/dbconfig/@*")
If l_Iterator.MoveNext() = True Then
Dim nav2 As System.Xml.XPath.XPathNavigator =
l_Iterator.Current.Clone()
nav2.MoveToFirstAttribute()
l_strText = nav2.Value
While (nav2.MoveToNextAttribute())
l_strText = nav2.Value
End While
End If
Catch ex As Exception
System.Console.WriteLine("Exception: " + ex.Message)
End Try