472,976 Members | 1,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

Change XmlDocument Function to XPathDocument Function

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
Nov 12 '05 #1
2 2444
Jim Lewis wrote:

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


This looks weird. Current node is first of /configuration/dbconfig/@* -
attribute, so calling MoveToFirstAttribute() on it isn't good idea.
Try
Dim l_Iterator As System.Xml.XPath.XPathNodeIterator =
l_XPathNavigator.Select("/configuration/dbconfig")

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
If you want to process each attribute, the following should work:
Dim iterator As XPathNodeIterator =
navigator.Select("/configuration/dbconfig/@*")

Do While (iterator.MoveNext())
Console.WriteLine("The attribute name is {0} and its value is {1}",
iterator.Current.LocalName, iterator.Current.Value)
End

You don't need to move to attributes because you're already iterating
attributes.
I'm a C# guy, so there may be something wrong with the While loop :S

HTH

--
Daniel Cazzulino [XML MVP]
Lagash Systems SA
http://weblogs.asp.net/cazzu
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
Jim Lewis wrote:

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


This looks weird. Current node is first of /configuration/dbconfig/@* -
attribute, so calling MoveToFirstAttribute() on it isn't good idea.
Try
Dim l_Iterator As System.Xml.XPath.XPathNodeIterator =
l_XPathNavigator.Select("/configuration/dbconfig")

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ian Lawton | last post by:
Hi, in a C# app, I have an XPathDocument that looks similar to this: <row uniqueid="4234" /> <row uniqueid="4365" /> <row uniqueid="3124" /> <row uniqueid="9879" /> <row uniqueid="1332" />...
2
by: Ed S | last post by:
Hi All, I'd like to convert from an XmlDocument to an XPathDocument (without saving the XmlDocument down to a file - this is a web app). I can't see any way to do this. Any help is...
2
by: Graham Pengelly | last post by:
Hi I am trying to transform on System.Xml.XmlDocument into another using XslTransform without writing the object out to a file. I am guessing it should work something like this... public...
3
by: Fahad Ashfaque | last post by:
Hi, I found the XSL Transformation through .NET too complex. The most simplest overload of System.Xml.Xsl.XslTransform is Transform(inputfile, outputfile), but this saves the result into file. I...
2
by: Rick | last post by:
I have an XML document that is generated from Infopath, I need to change the value of a namespace that is defined in a node in the form: <xsf:xDocumentClass "xmlns:my=valuehere">. when i navigate...
5
by: Fahad Ashfaque | last post by:
Hi, I've an object of XmlDocument, I need to pass it to some function which takes XpathDocument object, How could I convert? There is not Conversion Operator overload like that. Your reply...
1
by: Greg Allen | last post by:
Is there a way to get line number information from an XmlDocument object? As I work with the various elements in the XmlDocument, I want to be able to refer to the line number. I found an...
5
by: Ben R. | last post by:
Hi, Could someone explain the functional difference between these classes? From what I understand, xpathdocument is faster in some scenarios, but I'm not sure why. Further, why is it that the...
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've been trying to improve the performance of our xml handling, so I've been doing some timing tests lately and ran across something I thought odd - XPathDocument seems slightly slower...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.