473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2464
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.