473,385 Members | 1,872 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.

node value keeps vanishing with XML.xmlTextReader

hi all

first off, i'm not trying to cross post, but couldn't find this newsgroup
earlier (got here from a recommendation on microsoft.public.vb, where i
originally posted this question).

anyhow, i'm just getting into .Net and am trying to parse a document, but
not able to read the values for the nodes consistently. anybody know what
i'm doing wrong? it seems that i'm able to console.writeLine the value of
the node at the beginning of my Read() loop, but by the time i'm down a
couple of IF ELSE branches and want to assign the node value into a variable
(to be used if an SQL statement), i'm not unable to read the value again. is
this a bug?

tks

Imports System.Xml
Imports System.Data.SqlClient
Imports System
Imports System.IO

Module Module1
Dim sFileName As String
Dim sConn As String
Dim oConn As SqlConnection
Dim oCmd As SqlCommand
Dim dDate As Date
Dim oXmlReader As Xml.XmlTextReader
Dim bTestMode As Boolean
Dim sNodeName As String
Dim sDocketNode As String
Dim iDocket As Integer
Dim sCustomerName As String
Dim sDescription As String
Dim sCustomerNode As String
Dim sJobNode As String
Dim sNodeValue As String
Dim sSql As String
Dim iCompanyID As Integer
Sub Main()
bTestMode = True
If bTestMode = True Then
sFileName = New String("G:\dtech_data.xml")
Else
sFileName = New String("d:\dtech_data.xml")
End If
dDate = System.DateTime.Now

Try
oXmlReader = New Xml.XmlTextReader(sFileName)
oXmlReader.WhitespaceHandling = Xml.WhitespaceHandling.None
If oXmlReader.Read Then '// data file found and opened okay //
sConn = New String("Data Source=localhost;User
ID=sa;Password=; initial catalog=db_name")
oConn = New SqlConnection(sConn)
oConn.Open()
sDocketNode = New String("Docket")
sCustomerNode = New String("Customer")
sJobNode = New String("Job")

While oXmlReader.Read()
sNodeName = oXmlReader.Name()
Select Case (oXmlReader.NodeType)
Case XmlNodeType.Text
Console.WriteLine("*** NODE NAME: " &
oXmlReader.Name & ". NODE VALUE: " & oXmlReader.Value & " ***")
If sNodeName.Equals(sDocketNode) Then
iDocket = oXmlReader.GetAttribute("ID")
Console.WriteLine("Found docket node with
value of: " & iDocket.ToString())
ElseIf sNodeName.Equals(sCustomerNode) Then
sCustomerName = oXmlReader.Value
Console.WriteLine("Found customer node with
value of " & sCustomerName)
ElseIf sNodeName.Equals(sJobNode) Then
sDescription = oXmlReader.Value
Console.WriteLine("Found job node with value
of " & sDescription)
sSql = "insert into project (docket,
companyID, description) values (" & iDocket & ", " & iCompanyID & ", '" &
sDescription & "'"
oCmd = New SqlCommand(sSql, oConn)
'oCmd.ExecuteNonQuery()
End If
End Select
End While

End If
Catch ex As Exception
Console.WriteLine("Error - " & ex.ToString())
Finally '// clean up //
If Not (oXmlReader Is Nothing) Then
oXmlReader.Close()
End If
If Not (oConn Is Nothing) Then
oConn.Close()
End If
End Try
End Sub
End Module

Nov 21 '05 #1
1 1770
"Dica" <ge*****@hotmail.com> wrote in message
news:qL********************@rogers.com...
hi all

first off, i'm not trying to cross post, but couldn't find this newsgroup
earlier (got here from a recommendation on microsoft.public.vb, where i
originally posted this question).

anyhow, i'm just getting into .Net and am trying to parse a document, but
not able to read the values for the nodes consistently. anybody know what
i'm doing wrong? it seems that i'm able to console.writeLine the value of
the node at the beginning of my Read() loop, but by the time i'm down a
couple of IF ELSE branches and want to assign the node value into a variable (to be used if an SQL statement), i'm not unable to read the value again. is this a bug?

tks
after some additional mucking about, it looks like this problem is cursor
related. the very process of reading data from a node moves the cursor ahead
one, so when i grab the value from the node in my code below, i was moving
on to the next node, prior to the next oXmlReader.Read(), which i failed to
understand.


Imports System.Xml
Imports System.Data.SqlClient
Imports System
Imports System.IO

Module Module1
Dim sFileName As String
Dim sConn As String
Dim oConn As SqlConnection
Dim oCmd As SqlCommand
Dim dDate As Date
Dim oXmlReader As Xml.XmlTextReader
Dim bTestMode As Boolean
Dim sNodeName As String
Dim sDocketNode As String
Dim iDocket As Integer
Dim sCustomerName As String
Dim sDescription As String
Dim sCustomerNode As String
Dim sJobNode As String
Dim sNodeValue As String
Dim sSql As String
Dim iCompanyID As Integer
Sub Main()
bTestMode = True
If bTestMode = True Then
sFileName = New String("G:\dtech_data.xml")
Else
sFileName = New String("d:\dtech_data.xml")
End If
dDate = System.DateTime.Now

Try
oXmlReader = New Xml.XmlTextReader(sFileName)
oXmlReader.WhitespaceHandling = Xml.WhitespaceHandling.None
If oXmlReader.Read Then '// data file found and opened okay //
sConn = New String("Data Source=localhost;User
ID=sa;Password=; initial catalog=db_name")
oConn = New SqlConnection(sConn)
oConn.Open()
sDocketNode = New String("Docket")
sCustomerNode = New String("Customer")
sJobNode = New String("Job")

While oXmlReader.Read()
sNodeName = oXmlReader.Name()
Select Case (oXmlReader.NodeType)
Case XmlNodeType.Text
Console.WriteLine("*** NODE NAME: " &
oXmlReader.Name & ". NODE VALUE: " & oXmlReader.Value & " ***")
If sNodeName.Equals(sDocketNode) Then
iDocket = oXmlReader.GetAttribute("ID")
Console.WriteLine("Found docket node with
value of: " & iDocket.ToString())
ElseIf sNodeName.Equals(sCustomerNode) Then
sCustomerName = oXmlReader.Value
Console.WriteLine("Found customer node with value of " & sCustomerName)
ElseIf sNodeName.Equals(sJobNode) Then
sDescription = oXmlReader.Value
Console.WriteLine("Found job node with value of " & sDescription)
sSql = "insert into project (docket,
companyID, description) values (" & iDocket & ", " & iCompanyID & ", '" &
sDescription & "'"
oCmd = New SqlCommand(sSql, oConn)
'oCmd.ExecuteNonQuery()
End If
End Select
End While

End If
Catch ex As Exception
Console.WriteLine("Error - " & ex.ToString())
Finally '// clean up //
If Not (oXmlReader Is Nothing) Then
oXmlReader.Close()
End If
If Not (oConn Is Nothing) Then
oConn.Close()
End If
End Try
End Sub
End Module

Nov 21 '05 #2

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

Similar topics

6
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
4
by: Roshan | last post by:
I'm newbie to xml and C#. I have one XML file with the following content: <Store> <Book id="1" > <Title>Thermodynamics Unleashed</Title> <Price>56.00</Price> <Book>
5
by: juli jul | last post by:
Hello, How can I get a xml node from a XmlTextReader? Thanks a lot! *** Sent via Developersdex http://www.developersdex.com ***
3
by: Scott M. Lyon | last post by:
I'm trying to figure out the best way (considering there could be instances where I get a lot of data in this XML, and I want to minimize any slowdowns) to extract the value of one particular node...
6
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET...
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
3
by: melanieab | last post by:
Hi, I can't seem to get the syntax correct. I have an xmlnodelist full of elements called "file". Each file has an attribute called "ID". If I just want to get, say, the ID of the second file...
4
by: CodeRazor | last post by:
I am reading an xml file from a URL. I was originally doing this using the XmlDocument class. But this was very slow. The XmlTextReader is meant to be much quicker for forward only retrieval of...
3
by: raghudr | last post by:
Hi all, I am parsing a .xml file.My main intention is to retrieve the name value of node "Signal":- "Name Value" which is "rag". i want to store only the <signal"name value" that is only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.