D
Dica
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
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